compare numbers with different decimal point

Get help with using AutoHotkey and its commands and hotkeys
Albireo
Posts: 244
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: 613
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] [Expand] [Download] GeSHi © Codebox Plus



Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus

Last edited by jeeswg on 20 Mar 2017, 07:25, edited 1 time in total.
User avatar
jeeswg
Posts: 613
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] [Expand] [Download] GeSHi © Codebox Plus

Albireo
Posts: 244
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] [Expand] [Download] GeSHi © Codebox Plus

Albireo
Posts: 244
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: 613
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] [Expand] [Download] GeSHi © Codebox Plus



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] [Download] GeSHi © Codebox Plus

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:]
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] [Download] GeSHi © Codebox Plus

q::
vNum := "10.000,00"
vNum := StrReplace(vNum, ".", "")
vNum := StrReplace(vNum, ",", ".")
MsgBox % vNum
Return
Last edited by jeeswg on 20 Mar 2017, 08:15, edited 1 time in total.
Albireo
Posts: 244
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] [Expand] [Download] GeSHi © Codebox Plus

Albireo
Posts: 244
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: 613
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.

Return to “Ask For Help”

Who is online

Users browsing this forum: Google [Bot], lj87 and 27 guests