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

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

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

21 Nov 2018, 19:04

Note: This function was originally named Float() as the function was using Floating point option of Format()
Its now renamed as RoundT() being based on Round()

The reason for choosing Round() over Format() is that the latter seems to be less accurate.

Code: Select all

MsgBox % Format( "{:0.3f}", 1.2345 ) ; 1.234 <- incorrect? 
         . "`n" . Round( 1.2345, 3 ) ; 1.235 <- correct!

RoundT() is a simple wrapper for Round(). It works similar to Round() except it trims redundant trailing zeroes from the result,

RoundT( n, p )
n is any number or a numerical result from an mathematical expression.
p is the floating point precision of the result. Max allowed precision is 15, but try to keep it low, like 10 or less.

Round() can produce undesirable results when precision is too high, for eg: MsgBox % Round( 100, 19 ) ; returns 99.9999999996264960000

The Function with sample calls:

Code: Select all

RoundT( n, p:=6 ) { ; By SKAN on D289 @ goo.gl/Q7zQG9
Return ( p<1 ? Round(n,p) : SubStr(n:=Round(n, p), 1, -1-p) ) 
    .  ( p<1 ? "" : (n:=RTrim(SubStr(n, 1-p, p), 0))  ? ("." . n) : "" ) 
}

; usage samples
MsgBox % RoundT( -000000123.4560000  ) ; removes redundant leading/trailing zeroes
MsgBox % RoundT( 100.)                 ; removes the dot
MsgBox % RoundT( 100 )                 ; changes nothing
MsgBox % RoundT( 86400/24 )            ; Total minutes in a day
MsgBox % RoundT( 123.56, 0 )           ; Rounds off to integer
MsgBox % RoundT( Sqrt(2), 15 )         ; maximum precision of 15
My Scripts and Functions: V1  V2
User avatar
Thoughtfu1Tux
Posts: 125
Joined: 31 May 2018, 23:26

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

22 Nov 2018, 00:35

Does this not accept variables? I could not get this to work with a variable.
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

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

22 Nov 2018, 05:33

Can you provide a simple example?
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

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

22 Nov 2018, 05:59

The following works for me

Code: Select all

#NoEnv
#Warn
#SingleInstance, Force

A := 0.0000001
B := 0.0000001
MsgBox % A+B               ; 0.000000  since default precision 0.6 is not enough
MsgBox % Float( A+B, 7  )  ; 0.0000002
User avatar
Thoughtfu1Tux
Posts: 125
Joined: 31 May 2018, 23:26

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

23 Nov 2018, 14:47

Ah, that did it. I made a silly mistake when testing my code.
Thank you! This will come in really handy for when I’m using COM to grab .number values from excel, as that always gives me a bunch of extra zeroes at the end.
carno
Posts: 265
Joined: 20 Jun 2014, 16:48

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

25 Nov 2018, 03:20

Excellent function. Definitely will be put to good use. :)
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

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

27 Nov 2018, 08:38

In help doc, SubStr() doesn't have enough examples to show how powerful this function is.

The example below shows possibilities with SubStr(), and how Float() is splitting a number to decimal and fraction:

Code: Select all

#NoEnv
#Warn
#SingleInstance, Force

d := "24September1980" ; Variable length, can be as short as 01May1980
d1 := SubStr(d,1,2)    ; Extract from position 1 upto 2 chars  
d2 := SubStr(d,3,-4)   ; Extract from postion 3 all but last 4 chars
d3 := SubStr(d,-3)     ; Extract from postion -3 (4th char from rear) all chars
d4 := SubStr(d,-3,2)   ; Extract from postion -3 (4th char from rear) upto 2 chars

MsgBox % d1 "-" d2 "-" d3 
MsgBox % "Century=" d4 


n := 02.100                 ; number

n := Format( "{:06f}", n )  ; n formatted to fixed fraction length of 6 digits. Leading zeroes are cleared  
n1 := SubStr( n, 1, -7 )    ; Extract all except last 7 chars
n2 := SubStr( n,-5 )        ; Extract only last 6 Chars
nn := RTrim(n2,0)           ; Removes trailing zeroes from fraction

MsgBox % "Number:`t" n "`nDecimal:`t" n1 "`nFraction:`t" n2
MsgbOx % "Formatted:`t" n "`nZeroTrimmed:`t" n1 . ( nn ? "." nn : "" )
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Float() error in older version

06 Aug 2019, 05:11

I wasn't aware Format() can be used to round off to integer

Code: Select all

MsgBox % Format( "{:0.0f}", 123.56 )  ; 124
The previous version was giving erroneous result when precision was 0 (or less)

Code: Select all

MsgBox % Round(123.56, 0) ; 124    <- Correct
MsgBox % Float(123.56, 0) ; 12.124 <- Error


Float( n, p:=6 ) { ; By SKAN on D1BM @ goo.gl/Q7zQG9
Return SubStr(n:=Format("{:0." p "f}",n),1,-1-p) . ((n:=RTrim( SubStr(n,1-p),0) ) ? "." . n : "") 
}
Float() has been updated to behave like Round() when zero precision is required.
Please refer the Title post for updated function.
My Scripts and Functions: V1  V2
User avatar
Delta Pythagorean
Posts: 627
Joined: 13 Feb 2017, 13:44
Location: Somewhere in the US
Contact:

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

07 Aug 2019, 15:56

Doesn't Round() do the same thing in a way? Or am I mistaken?

[AHK]......: v2.0.12 | 64-bit
[OS].......: Windows 11 | 23H2 (OS Build: 22621.3296)
[GITHUB]...: github.com/DelPyth
[PAYPAL]...: paypal.me/DelPyth
[DISCORD]..: tophatcat

User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

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

07 Aug 2019, 16:49

Delta Pythagorean wrote:
07 Aug 2019, 15:56
Doesn't Round() do the same thing in a way? Or am I mistaken?
Yes. Float() is same as Round() except it will remove redundant trailing zeroes from any fraction.

Or, are asking me why I chose Format() over Round() inside my function?
I used it because doc for SetFormat suggested it.
I don't know which is faster. I think Round() will be more accurate than Format(), but slower.

Thank you for bringing this up. I need to check this.
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

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

08 Aug 2019, 17:11

Update: Function rewritten and renamed.
carno
Posts: 265
Joined: 20 Jun 2014, 16:48

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

17 Aug 2019, 12:00

Thanks for the update. I have been following SKAN's incredible posts for years!
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

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

06 Aug 2021, 11:11

SubStr() is broken in V2, hence RoundT() too.

Here is an alternate version for V1/V2

Code: Select all

RoundT(n, p:=7) {     ; By SKAN on D289/D486 @ goo.gl/Q7zQG9
Return p<1 ? Round(n, p) : RTrim(RTrim(Round(n, p),"0"),".")
}
safetycar
Posts: 435
Joined: 12 Aug 2017, 04:27

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

07 Aug 2021, 00:13

SKAN wrote:
06 Aug 2021, 11:11
SubStr() is broken in V2,
Could it be that you are having problems where you set the StartingPos? There are differences between v1 and v2.
In v1 last char is 0. In v2 last char is -1.
Links:
https://lexikos.github.io/v1/docs/commands/SubStr.htm
https://lexikos.github.io/v2/docs/commands/SubStr.htm
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

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

07 Aug 2021, 01:40

kczx3 wrote:
06 Aug 2021, 22:08
Huh? How is it broken in v2?
My choice of words for addressing a breaking change. :)
If I was posting in V2 section, I would see this as a change/enhancement.
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

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

07 Aug 2021, 01:51

safetycar wrote:
07 Aug 2021, 00:13
In v1 last char is 0. In v2 last char is -1.
Yes, I know.
I don't intend to explain V2 things in my V1 topics...
such as why RoundT() is still needed in V2.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 92 guests