Page 1 of 2

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

Posted: 02 Jul 2017, 15:22
by Suresh
Edited by SKAN: Better version here: RoundT()

Code: Select all

ZTrim( N := "" ) { ; SKAN /  CD:01-Jul-2017 | LM:03-Jul-2017 | Topic: goo.gl/TgWDb5
Local    V  := StrSplit( N, ".", A_Space ) 
Local    V0 := SubStr( V.1,1,1 ),   V1 := Abs( V.1 ),      V2 :=  RTrim( V.2, "0" )
Return ( V0 = "-" ? "-" : ""   )  ( V1 = "" ? 0 : V1 )   ( V2 <> "" ? "." V2 : "" )
}

;---------------------------------------------------------------------------------
; Example: 

MsgBox % ( A := 1.234000    ) "`t=`t" ZTrim( A ) "`n"
       . ( A := 00.00100    ) "`t=`t" ZTrim( A ) "`n"
       . ( A := -123.010    ) "`t=`t" ZTrim( A ) "`n"
       . ( A := -.212       ) "`t=`t" ZTrim( A ) "`n"
       . ( A := "+001. 100" ) "`t=`t" ZTrim( A ) "`n"
       . ( A := "1 .12300 " ) "`t=`t" ZTrim( A ) "`n"
       . ( A := 100.00      ) "`t=`t" ZTrim( A ) "`n"
       . ( A := 100         ) "`t=`t" ZTrim( A ) "`n"                                   

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

Posted: 02 Jul 2017, 16:13
by wolf_II
Very nice function, thanks for sharing. :bravo:

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

Code: Select all

V := StrSplit( N,  .,  A_Space )
versus
V := StrSplit( N, ".", A_Space )

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

Posted: 02 Jul 2017, 16:25
by Suresh
wolf_II wrote:Is it a bug or a feature? :o :think:
A mistake.. :(

Edited.. Thanks for pointing it :)

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

Posted: 02 Jul 2017, 16:28
by wolf_II
Suresh wrote:A mistake.. :(
It worked though! :D

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

Posted: 02 Jul 2017, 16:37
by Suresh
wolf_II wrote:It worked though! :D
I'm surprised, But only the dot works.. Accidental discovery. :shock:

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

Posted: 03 Jul 2017, 03:20
by carno
Very useful!

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

Posted: 03 Jul 2017, 03:36
by SpeedMaster
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

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"

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

Posted: 03 Jul 2017, 04:02
by Helgef
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

Code: Select all

msgbox, % .

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

Posted: 03 Jul 2017, 04:24
by jNizM
@SpeedMaster
For currency you should use GetCurrencyFormat / GetCurrencyFormatEx or GetNumberFormat / GetNumberFormatEx for numbers with normal number format 2,999.99 / 2.999,99

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

Posted: 03 Jul 2017, 04:36
by Suresh
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

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.

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

Posted: 03 Jul 2017, 04:41
by Suresh
Helgef wrote:I'm literally surprised too :o

Code: Select all

msgbox, % .
:D

Code: Select all

msgbox, % "A" . "B"
Works properly when concatenating.. I wouldn't know as I rarely use dot to concatenate strings.

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

Posted: 03 Jul 2017, 05:44
by SpeedMaster
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

dot=`a
msgbox, % dot


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

Posted: 03 Jul 2017, 05:48
by Helgef
SpeedMaster wrote: how about this ? :crazy:

Code: Select all

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.

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

Posted: 03 Jul 2017, 06:59
by SpeedMaster
Ah! it's a bell, I thought it was a dot. :D
Here the result with the whole alphabet :

Code: Select all

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 

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

Posted: 05 Jul 2017, 06:32
by Suresh
Helgef wrote:For circular reference, see Dot interpreted as literal string even when unquoted.
Thanks for the topic. Good ,this has been clarified.

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

Posted: 05 Jul 2017, 19:03
by FanaticGuru
Not that it matters much but I just use a RegEx to get rid of trailing zeros.

Code: Select all

; 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

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

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

Posted: 13 Jul 2017, 08:57
by jeeswg
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

q::
;crop leading zeros (maintain minus sign, keep a 0 if required)
vList := "1000,-1000,000,-000,00.00,-00.00,0.0,-0.0,00123,-00123,00123.123,-00123.123,00.123,-00.123,2.00500,-2.00500,2.500,-2.500,2.000,-2.000,0002000.0002000,-0002000.0002000"
vOutput := ""
Loop, Parse, vList, % ","
{
	vNum := A_LoopField
	vOutput .= vNum "`t`t" RegExReplace(vNum, "^-?\K0+(?=[^0.]|0\.|0$)") "`r`n"
	;vOutput .= vNum "`t`t" RegExReplace(vNum, "^-?\K0+") "`r`n" ;never keeps a leading zero
}
;Clipboard := vOutput
MsgBox, % vOutput

;crop trailing zeros after decimal point (remove decimal point if required)
vOutput := ""
Loop, Parse, vList, % ","
{
	vNum := A_LoopField
	vOutput .= vNum "`t`t" RegExReplace(vNum, "\.0+$|\..*?\K0+$") "`r`n"
}
;Clipboard := vOutput
MsgBox, % vOutput

;crop leading and trailing zeros
vOutput := ""
Loop, Parse, vList, % ","
{
	vNum := A_LoopField
	vOutput .= vNum "`t`t" RegExReplace(vNum, "^-?\K0+(?=[^0.]|0\.|0$)|\.0+$|\..*?\K0+$") "`r`n"
	;vOutput .= vNum "`t`t" ZTRim(vNum) "`r`n"
}
;Clipboard := vOutput
MsgBox, % vOutput
return

;==================================================

;ZTrim() : Remove redundant leading/trailing zeroes from a number - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=6&t=33960

ZTrim( N := "" ) { ; SKAN /  CD:01-Jul-2017 | LM:03-Jul-2017 | Topic: goo.gl/TgWDb5
Local    V  := StrSplit( N, ".", A_Space )
Local    V0 := SubStr( V.1,1,1 ),   V1 := Abs( V.1 ),      V2 :=  RTrim( V.2, "0" )
Return ( V0 = "-" ? "-" : ""   )  ( V1 = "" ? 0 : V1 )   ( V2 <> "" ? "." V2 : "" )
}

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

Posted: 17 Jul 2017, 04:10
by Suresh
FanaticGuru wrote:Not that it matters much but I just use a RegEx to get rid of trailing zeros.

Code: Select all

; 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.

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

Posted: 17 Jul 2017, 04:25
by Suresh
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.

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

Posted: 21 Jul 2017, 14:58
by FanaticGuru
It would seem that a simple Format will handle most of what is needed.

Code: Select all

ZZTrim(N := "")
{
	return Format("{1:g}", N) 
}

;---------------------------------------------------------------------------------
; Example: 
MsgBox % ( A := 1.234000    ) "`t=`t" ZZTrim( A ) "`n"
       . ( A := 00.00100    ) "`t=`t" ZZTrim( A ) "`n"
       . ( A := -123.010    ) "`t=`t" ZZTrim( A ) "`n"
       . ( A := -.212       ) "`t=`t" ZZTrim( A ) "`n"
       . ( A := "+001.100" ) "`t=`t" ZZTrim( A ) "`n"
       . ( A := "1.12300 " ) "`t=`t" ZZTrim( A ) "`n"
       . ( A := 100.00      ) "`t=`t" ZZTrim( A ) "`n"
       . ( A := 100         ) "`t=`t" ZZTrim( A ) "`n"                                     

This expects an actually number recognized by AHK with no spaces, dollar signs, or reversed comma and decimal signs.

No script is going to be able to handle reversed comma and decimals without some kind of assumptions or additional information. Is 1,234 a number less than 2 or more than a 1000?

FG