compare numbers with different decimal point

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Albireo
Posts: 1748
Joined: 16 Oct 2013, 13:53

compare numbers with different decimal point

20 Mar 2017, 04:27

Hi!
I want to compare numbers with different decimal point.
For example .:
This is equal
13.19 = 13,19
0.250000 = 0,25
29 = 29,00

I only need to compare maximum 2 decimals.
(The second value, with decimal comma is always two decimals)
I can't say (right now) if both values are strings or one of them is numbers.
My desire is to know if it is the same value.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: compare numbers with different decimal point

20 Mar 2017, 05:36

If two items look numeric, they will be compared as numeric. If at least one item looks non-numeric or for example "" is used to force an item to be seen as text, then the items are compared as text.

Sometimes I like to wait to give other people a chance to chip in. It's been a while so here goes:

Code: Select all

q:: ;compare numbers (dot v. comma decimal point notation)
vText = ;continuation section
(
13.19 13,19
0.250000 0,25
29 29,00
1.1 2,0
)

Loop, Parse, vText, `n
{
	vTemp := A_LoopField
	StringSplit, vTemp, vTemp, %A_Space%
	vTemp1X := StrReplace(vTemp1, ",", ".")
	vTemp2X := StrReplace(vTemp2, ",", ".")
	MsgBox % (vTemp1X = vTemp2X) "`r`n" vTemp1 "`r`n" vTemp2
}
Return

Code: Select all

q:: ;some variable comparison tests
;MsgBox % (01 = 1) ;1
;MsgBox % ("" 01 = 1) ;0
;MsgBox % (01 = "" 1) ;0
;MsgBox % ("" 01 = "" 1) ;0

MsgBox % "a " (01 = 1) ;1
MsgBox % "b " ("" 01 = 1) ;0
MsgBox % "c " (01 = "" 1) ;0
MsgBox % "d " ("" 01 = "" 1) ;0

MsgBox % "e " ("01" = 1) ;0
MsgBox % "f " (01 = "1") ;0
MsgBox % "g " ("01" = "1") ;0

vText1 := "01"
vText2 := "1"
;careful: the variables were defined as text, but will be seen as numeric (and compared as numeric) in the first test below
MsgBox % "h " (vText1 = vText2) ;1
MsgBox % "i " ("" vText1 = vText2) ;0
MsgBox % "j " (vText1 "" = vText2) ;0
MsgBox % "k " (vText1 = "" vText2) ;0
MsgBox % "l " ("" vText1 = "" vText2) ;0

vText1 := 01
vText2 := 1
MsgBox % vText1 ;01
MsgBox % "" vText1 ;01

MsgBox % "m " (vText1 = vText2) ;1
MsgBox % "n " ("" vText1 = vText2) ;0
MsgBox % "o " (vText1 "" = vText2) ;0
MsgBox % "p " (vText1 = "" vText2) ;0
MsgBox % "q " ("" vText1 = "" vText2) ;0

vText1 := 01+0
vText2 := 1+0
MsgBox % vText1 ;1
MsgBox % "" vText1 ;1

MsgBox % "r " (vText1 = vText2) ;1
MsgBox % "s " ("" vText1 = vText2) ;1
MsgBox % "t " (vText1 "" = vText2) ;1
MsgBox % "u " (vText1 = "" vText2) ;1
MsgBox % "v " ("" vText1 = "" vText2) ;1
Return
Last edited by jeeswg on 20 Mar 2017, 07:25, edited 1 time in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: compare numbers with different decimal point

20 Mar 2017, 06:29

Surprisingly
vText += 0
and
vText := vText + 0
can give different answers, e.g. where vText := "a"
If anyone knows the explanation for this.

[EDIT:]
I did find:
Math Functions
https://autohotkey.com/docs/commands/Math.htm

Most math functions do not perform strict type-checking, so may treat non-numeric values as zero or another number. For example, Round("1.0foo") produces 1. However, this is expected to change in v2.0.

Code: Select all

;q:: ;further string/text tests
vText := "a"
vText++
MsgBox % vText ;1

vText := "a"
vText += 0
MsgBox % vText ;0

vText := "a"
vText := vText + 0
MsgBox % vText ;blank

vText := "a"
MsgBox % (vText + 0) ;blank

MsgBox % (vText = vText + 0) ;0

vText := "01"
MsgBox % JEE_StrLooksNumeric(vText) ;1
vText := "1"
MsgBox % JEE_StrLooksNumeric(vText) ;1
vText := "0xA"
MsgBox % JEE_StrLooksNumeric(vText) ;1
vText := "A"
MsgBox % JEE_StrLooksNumeric(vText) ;0
Return

JEE_StrLooksNumeric(vText)
{
Return !(vText + 0 = "")
}
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Albireo
Posts: 1748
Joined: 16 Oct 2013, 13:53

Re: compare numbers with different decimal point

20 Mar 2017, 07:17

Thanks!

This is almost the same thing? (I feel more easy to understand)
But maybe this way has several limitations?

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
#SingleInstance force
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.



SetFormat FloatFast, 0.2	; två decimaler med decimalpunkt

; Test1 - OK!
No1a := "13,02"
StringReplace No1a, No1a, `,, ., All
No1a += 0.00

No1b := 13.0200, No1b += 0.00

; Check result1
IfEqual No1a, %No1b%
	MsgBox 64, Rad %A_LineNo% -> %A_ScriptName%, Test1 `nThe Numbers is equal `n`nNo1a = %No1a% `nNo1b = %No1b%
else
	MsgBox 64, Rad %A_LineNo% -> %A_ScriptName%, Test1 `nThe Numbers is NOT equal `n`nNo1a = %No1a% `nNo1b = %No1b%



; Test2 - OK
No1a := "29,00"
StringReplace No1a, No1a, `,, ., All
No1a += 0.00

No1b := 29, No1b += 0.00

; Check result2
IfEqual No1a, %No1b%
	MsgBox 64, Rad %A_LineNo% -> %A_ScriptName%, Test2 `nThe Numbers is equal `n`nNo1a = %No1a% `nNo1b = %No1b%
else
	MsgBox 64, Rad %A_LineNo% -> %A_ScriptName%, Test2 `nThe Numbers is NOT equal `n`nNo1a = %No1a% `nNo1b = %No1b%



; Test3 - OK
No1a := "0,25"
StringReplace No1a, No1a, `,, ., All
No1a += 0.00

No1b := 0.250000, No1b += 0.00

; Check result3
IfEqual No1a, %No1b%
	MsgBox 64, Rad %A_LineNo% -> %A_ScriptName%, Test3 `nThe Numbers is equal `n`nNo1a = %No1a% `nNo1b = %No1b%
else
	MsgBox 64, Rad %A_LineNo% -> %A_ScriptName%, Test3 `nThe Numbers is NOT equal `n`nNo1a = %No1a% `nNo1b = %No1b%

ExitApp
Albireo
Posts: 1748
Joined: 16 Oct 2013, 13:53

Re: compare numbers with different decimal point

20 Mar 2017, 08:01

I got another problem!
The number with a decimal point is in a field (as I can read - for example 13,02)
And the right value (for example 13.02) is in a CSV-file, and I want to compare this two values.
Now it has become a bit more complicated.
When the value in the field is over 1000, have the number, the following format 1.000,00
ie "10.000,00" in the field, should be compared with the "10000" from the file.

//jan
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: compare numbers with different decimal point

20 Mar 2017, 08:08

Yes, I think the scripts are quite similar. I would avoid using IfEqual, as it won't be available in AutoHotkey v2. I'd have hoped people would find my script pretty simple though! I try to keep them tidy and reasonably self-explanatory with good variable names.

I have remade my script, using 'same' and 'different' instead of '1' and '0'. I have also put in some lines that allow the numbers to be either rounded or truncated, before they are compared numerically.

Code: Select all

;q:: ;compare numbers (dot v. comma decimal point notation)
vText = ;continuation section
(
13.19 13,19
0.250000 0,25
29 29,00
1.1 2,0
1.230 1,239
)

Loop, Parse, vText, `n
{
	vTemp := A_LoopField
	StringSplit, vTemp, vTemp, %A_Space%
	vTemp1X := StrReplace(vTemp1, ",", ".")
	vTemp2X := StrReplace(vTemp2, ",", ".")

	;round to 2dp
	vTemp1X := Round(vTemp1X, 2), vTemp2X := Round(vTemp2X, 2)
	;round to 2dp alternative
	;vTemp1X := Format("{:0.2f}", vTemp1X), vTemp2X := Format("{:0.2f}", vTemp2X)
	;truncate to 2dp (didn't work) (bug?)
	;vTemp1X -= Mod(vTemp1X, 0.01), vTemp2X -= Mod(vTemp2X, 0.01)
	;truncate to 2dp (alternative)
	;RegExMatch(vTemp1X, "^-?\d+.\d{1,2}|^-?\d+$", vTemp1X), RegExMatch(vTemp2X, "^-?\d+.\d{1,2}|^-?\d+$", vTemp2X)

	MsgBox % ((vTemp1X = vTemp2X) ? "same" : "different") "`r`n" vTemp1 " (" vTemp1X ")" "`r`n" vTemp2 " (" vTemp2X ")"
}
Return
In trying to find methods to truncate a number to 2dp, I think I have found a bug in AutoHotkey:

(Unless I have missed something in the documentation.)

Code: Select all

q:: ;a possible bug in AutoHotkey involving the Mod function
MsgBox % Mod(12.345, 0.01) ;0.005000
MsgBox % Mod(12.34, 0.01) ;0.010000 (bug?) (expected 0)
MsgBox % Mod(1.23, 0.01) ;0.010000 (bug?) (expected 0)
Return

;see: http://www.wolframalpha.com/input/?i=Mod(12.34,+0.01)
;see: http://www.wolframalpha.com/input/?i=Mod(1.23,+0.01)
[EDIT:] Mod issue discussed here:
Mod function returning wrong value? - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?t=29762

==================================================

[EDIT:]
For your new problem just do this:

Although I'm sure you've either already thought of this since posting, or probably would have done quite soon.

Code: Select all

q::
vNum := "10.000,00"
vNum := StrReplace(vNum, ".", "")
vNum := StrReplace(vNum, ",", ".")
MsgBox % vNum
Return
Last edited by jeeswg on 26 Apr 2017, 17:28, edited 2 times in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Albireo
Posts: 1748
Joined: 16 Oct 2013, 13:53

Re: compare numbers with different decimal point

20 Mar 2017, 08:09

Hmmm...
It wasn't as complicated as I first thought

Code: Select all

; Test4 - OK
SetFormat FloatFast, 0.2

No1a := "10.000,00"
StringReplace No1a, No1a, .,, All	; Remove all dots
StringReplace No1a, No1a, `,, ., All	; Replace all "," with "."
No1a += 0.00
No1b := 10000, No1b += 0.00

; Check result3
IfEqual No1a, %No1b%
	MsgBox 64, Rad %A_LineNo% -> %A_ScriptName%, Test4 `nThe Numbers is equal `n`nNo1a = %No1a% `nNo1b = %No1b%
else
	MsgBox 64, Rad %A_LineNo% -> %A_ScriptName%, Test4 `nThe Numbers is NOT equal `n`nNo1a = %No1a% `nNo1b = %No1b%	
Albireo
Posts: 1748
Joined: 16 Oct 2013, 13:53

Re: compare numbers with different decimal point

20 Mar 2017, 08:52

Thank You!
(You were a bit faster than me :-) )

Intresting about "IfEqual" and ahk v2. (Is v2 far away?)
Is it the same with StrReplace() and StringReplace....?
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: compare numbers with different decimal point

20 Mar 2017, 10:11

Yes, StringReplace as well. (Although in AHK v2 you can write every command as a function, and every function as a command. So ... the StrReplace command in AHK v2, is quite similar to the current StringReplace command.)

I wouldn't worry too much about conversion, I've converted lots of code/scripts, you just need a good converter, me and others are working on it. The vast majority of conversion can be automated, by converting certain command/function lines, and then inspecting the before/after results in WinMerge, to see that everything has converted successfully.

However, I think it's worth checking the changes, to avoid writing any new code that is awkward to convert, for me, that basically means avoid using multiple parameters on a Return line.

v2-changes
https://autohotkey.com/v2/v2-changes.htm

[EDIT:]
Btw SetFormat is due to be removed, personally I never use it, but you have used it a few times in your script, so you may want to look at the Format function.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Descolada, Haris00911 and 300 guests