ZTrim() : Remove redundant leading/trailing zeroes from a number

Post your working scripts, libraries and tools
Suresh
Posts: 31
Joined: 03 May 2016, 18:58

ZTrim() : Remove redundant leading/trailing zeroes from a number

02 Jul 2017, 15:22

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

Last edited by Suresh on 02 Jul 2017, 16:23, edited 1 time in total.
wolf_II
Posts: 1395
Joined: 08 Feb 2015, 20:55

Re: ZTrim() : Remove redundant leading/trailing zeroes from a number

02 Jul 2017, 16:13

Very nice function, thanks for sharing. :bravo:

Is it a bug or a feature? :o :think:

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

V := StrSplit( N,  .,  A_Space )
versus
V := StrSplit( N, ".", A_Space )
Suresh
Posts: 31
Joined: 03 May 2016, 18:58

Re: ZTrim() : Remove redundant leading/trailing zeroes from a number

02 Jul 2017, 16:25

wolf_II wrote:Is it a bug or a feature? :o :think:


A mistake.. :(

Edited.. Thanks for pointing it :)
wolf_II
Posts: 1395
Joined: 08 Feb 2015, 20:55

Re: ZTrim() : Remove redundant leading/trailing zeroes from a number

02 Jul 2017, 16:28

Suresh wrote:A mistake.. :(

It worked though! :D
Suresh
Posts: 31
Joined: 03 May 2016, 18:58

Re: ZTrim() : Remove redundant leading/trailing zeroes from a number

02 Jul 2017, 16:37

wolf_II wrote:It worked though! :D


I'm surprised, But only the dot works.. Accidental discovery. :shock:
SpeedMaster
Posts: 55
Joined: 12 Nov 2016, 16:09

Re: ZTrim() : Remove redundant leading/trailing zeroes from a number

03 Jul 2017, 03:36

Nice function but only works for the half of the world :(

https://en.wikipedia.org/wiki/Decimal_m ... arator.svg

Maybe add an option for the decimal Mark ? :roll:

ZTrim( N := "" , DM:="." ) :think:

Code: [Select all] [Download] (Untitled.ahk)GeSHi © Codebox Plus

MsgBox % ( A := 1,234000 euro   ) "`t=`t" ZTrim( A ) "`n"
. ( A := 00,00100 euro ) "`t=`t" ZTrim( A ) "`n"
. ( A :=100,10 ) "`t=`t" ZTrim( A ) "`n"
. ( A := 00100,001 € ) "`t=`t" ZTrim( A ) "`n"
Helgef
Posts: 1788
Joined: 17 Jul 2016, 01:02

Re: ZTrim() : Remove redundant leading/trailing zeroes from a number

03 Jul 2017, 04:02

Nice function, thanks for sharing. :thumbup:
Suresh wrote:
wolf_II wrote:It worked though! :D

I'm surprised, But only the dot works.. Accidental discovery. :shock:

I'm literally surprised too :o
User avatar
jNizM
Posts: 2154
Joined: 30 Sep 2013, 01:33
GitHub: jNizM

Re: ZTrim() : Remove redundant leading/trailing zeroes from a number

03 Jul 2017, 04:24

@SpeedMaster
For currency you should use GetCurrencyFormat / GetCurrencyFormatEx or GetNumberFormat / GetNumberFormatEx for numbers with normal number format 2,999.99 / 2.999,99
[AHK] 1.1.25.02 x64 Unicode
[WIN] 10 Pro (Version 1607 | Build 14393.1198) x64
My GitHub Profile | Donations are appreciated if I could help you
Suresh
Posts: 31
Joined: 03 May 2016, 18:58

Re: ZTrim() : Remove redundant leading/trailing zeroes from a number

03 Jul 2017, 04:36

SpeedMaster wrote:Nice function but only works for the half of the world :(


The intended use for ZTrim() is to counter the effect of using high floating point precision with SetFormat and Format()

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

SetFormat, Float, 0.12
A := 1 / 4
B := 4.1 * 100
C := 22 / 7


MsgBox % ( A := 1 / 4 ) "`t=`t" ZTrim( A ) "`n"
. ( B := 4.1 * 100 ) "`t=`t" ZTrim( B ) "`n"
. ( PI := 22 / 7 ) "`t=`t" ZTrim( PI ) "`n"


    ---------------------------
    Ztrim.ahk
    ---------------------------
    0.250000000000 = 0.25
    410.000000000000 = 410
    3.142857142857 = 3.142857142857

    ---------------------------
    OK
    ---------------------------

Ofcourse, you are free to alter the function anyway you want.
Suresh
Posts: 31
Joined: 03 May 2016, 18:58

Re: ZTrim() : Remove redundant leading/trailing zeroes from a number

03 Jul 2017, 04:41

Helgef wrote:I'm literally surprised too :o


:D



Works properly when concatenating.. I wouldn't know as I rarely use dot to concatenate strings.
SpeedMaster
Posts: 55
Joined: 12 Nov 2016, 16:09

Re: ZTrim() : Remove redundant leading/trailing zeroes from a number

03 Jul 2017, 05:44

Suresh wrote:The intended use for ZTrim() is to counter the effect of using high floating point precision with SetFormat() and and Format()


I now understand the purpose of this function thanks for your explanation :)

how about this ? :crazy:

Code: [Select all] [Download] (Untitled.ahk)GeSHi © Codebox Plus

dot=`a
msgbox, % dot

Helgef
Posts: 1788
Joined: 17 Jul 2016, 01:02

Re: ZTrim() : Remove redundant leading/trailing zeroes from a number

03 Jul 2017, 05:48

SpeedMaster wrote: how about this ? :crazy:

Code: [Select all] [Download] (Untitled.ahk)GeSHi © Codebox Plus

dot=`a
msgbox, % dot

#EscapeChar wrote:`a alert (bell) -- corresponds to Ascii value 7. It can also be manifest in some applications by typing Control+G.



For circular reference, see Dot interpreted as literal string even when unquoted.
SpeedMaster
Posts: 55
Joined: 12 Nov 2016, 16:09

Re: ZTrim() : Remove redundant leading/trailing zeroes from a number

03 Jul 2017, 06:59

Ah! it's a bell, I thought it was a dot. :D
Here the result with the whole alphabet :

Code: [Select all] [Download] (Untitled.ahk)GeSHi © Codebox Plus

chars=`a`b`c`d`e`f`g`h`i`j`k`l`m`n`o`p`q`r`s`t`u`v`w`x`y`z`.
msgbox, % chars
Suresh
Posts: 31
Joined: 03 May 2016, 18:58

Re: ZTrim() : Remove redundant leading/trailing zeroes from a number

05 Jul 2017, 06:32

Helgef wrote:For circular reference, see Dot interpreted as literal string even when unquoted.


Thanks for the topic. Good ,this has been clarified.
FanaticGuru
Posts: 638
Joined: 30 Sep 2013, 22:25

Re: ZTrim() : Remove redundant leading/trailing zeroes from a number

05 Jul 2017, 19:03

Not that it matters much but I just use a RegEx to get rid of trailing zeros.

Code: [Select all] [Download] (Untitled.ahk)GeSHi © Codebox Plus

; Remove Trailing Zeros
N := "100.0000"
MsgBox % RegExReplace(N, "\.0*$|(\.\d*?)0*$", "$1")


If you want a function, I guess you could do:

Code: [Select all] [Download] (Untitled.ahk)GeSHi © Codebox Plus

RemoveTrailingZeros(N)
{
return RegExReplace(N, "\.0*$|(\.\d*?)0*$", "$1")
}


It does not do leading zeros as I never had the need but that is pretty easy to add: RegExReplace(N, "^0*|\.0*$|(\.\d*?)0*$", "$1")

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts

AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon

Google Search, Dictionary, Thesaurus - Quickly Get Information from Specific Web Resources

[Function] Timer - Create and Manage Timers
User avatar
jeeswg
Posts: 1486
Joined: 19 Dec 2016, 01:58
Location: UK

Re: ZTrim() : Remove redundant leading/trailing zeroes from a number

13 Jul 2017, 08:57

It's tricky to do this in a RegEx one-liner, I'm not sure that any of the attempts above cover all of the list below for example, here's an attempt I made:

Any ideas/comments are welcome.

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

Suresh
Posts: 31
Joined: 03 May 2016, 18:58

Re: ZTrim() : Remove redundant leading/trailing zeroes from a number

17 Jul 2017, 04:10

FanaticGuru wrote:Not that it matters much but I just use a RegEx to get rid of trailing zeros.

Code: [Select all] [Download] (Untitled.ahk)GeSHi © Codebox Plus

; Remove Trailing Zeros
N := "100.0000"
MsgBox % RegExReplace(N, "\.0*$|(\.\d*?)0*$", "$1")


Very nice!. Removing trailing zeroes should be sufficient most of the times.
Thanks for sharing!

FanaticGuru wrote:It does not do leading zeros as I never had the need but that is pretty easy to add: RegExReplace(N, "^0*|\.0*$|(\.\d*?)0*$", "$1")


The solution doesn't handle negative number.
Suresh
Posts: 31
Joined: 03 May 2016, 18:58

Re: ZTrim() : Remove redundant leading/trailing zeroes from a number

17 Jul 2017, 04:25

jeeswg wrote:here's an attempt I made


Very nice! Thanks for sharing!

jeeswg wrote:Any ideas/comments are welcome.


A float like 0.01 is valid in ahk in these forms: +00.01 / +0.01 / .01 / +.01 / .010
ZTrim() calls Abs() to remove the redundant + sign, adds a leading zero for values like .01

ZTrim() will return 0 when param is either blank or invalid. This may or may not be desirable.

Return to “Scripts and Functions”

Who is online

Users browsing this forum: arcticir and 11 guests