output only necessary decimal places Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
bazkeys
Posts: 98
Joined: 20 Jan 2021, 21:58

output only necessary decimal places

20 Sep 2021, 00:50

I wanted to output a number to a tooltip, the number can vary, but is always positive and is always a multiple of .1
Anyway I noticed though I started with number 2, it outputs as 2.000000, 2.100000, 2.200000.... 3.000000 and so on.
I want the format to display 2,2.1,2.2,....3

Edit: Found the round function

Code: Select all

msgbox % Round(some_num,1)
So this successfully limits it to one decimal place.
That's ok for the majority of my purposes, but what if I want to display 3.142 as 3.142 (and not 3.1), but 3.1 as 3.1, 3.15 as 3.15 (and not 3.1), 3 as 3 (and not 3.0), basically displaying the full number but not any unnecessary trailing zeroes
Rohwedder
Posts: 7613
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: output only necessary decimal places  Topic is solved

20 Sep 2021, 01:24

Hallo,
try:

Code: Select all

No = 3.1420000
MsgBox,% beautified_N(No)

beautified_N(N, R:="")
{
    N := R!="" ? Round(N,Min(R,13)) : N
    N := SubStr(N,1+Neg := InStr(N,"-"))
    N := InStr(N := LTrim(N,"+0"),".") ? RTrim(N := RTrim(N,"0"),".") : N
    Return, (Neg and N ? "-": "") (SubStr(N,1,1)="."? 0 : "") (N=""? 0 : N)
}
bazkeys
Posts: 98
Joined: 20 Jan 2021, 21:58

Re: output only necessary decimal places

20 Sep 2021, 01:25

Found this old post which has a solution, it does seem a lot of work to have to make a function to perform what should be simple, but anyway I'm happy I found.

https://autohotkey.com/board/topic/11588-remove-trailing-zeros-from-var-solved/page-2
bazkeys
Posts: 98
Joined: 20 Jan 2021, 21:58

Re: output only necessary decimal places

20 Sep 2021, 01:28

@Rohwedder

Thanks, I plugged that into my code and it works :D

Solution I found on page 2 of the old post I linked also seems to work.
Rohwedder
Posts: 7613
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: output only necessary decimal places

20 Sep 2021, 01:49

really?

Code: Select all

Number =
(
-12.30
+012.000
012.00
-0012.00
-1.
00.
.00
+0.0
-0.0
0
.300
+.3
-.30
000.400
+000.400
-00000.401234
test
00050.0000000000000144444444444000
700
+700
-700
0700
+0700
-0700
)
Loop, Parse, Number, `n
    Text .= "`n" A_LoopField  "`t " zerotrimmer(A_LoopField) "`n`t" beautified_N(A_LoopField)
MsgBox,% Text

beautified_N(N, R:="")
{
    N := R!="" ? Round(N,Min(R,13)) : N
    N := SubStr(N,1+Neg := InStr(N,"-"))
    N := InStr(N := LTrim(N,"+0"),".") ? RTrim(N := RTrim(N,"0"),".") : N
    Return, (Neg and N ? "-": "") (SubStr(N,1,1)="."? 0 : "") (N=""? 0 : N)
}
zerotrimmer(num) {
 If num IS space
 IfNotInString num, .
Return num
  Loop, % StrLen(num)
  {
    StringRight the_number, num, 1
    If (the_number = ".")
     {
      StringTrimRight, num, num, 1
	Break
     }
    If (the_number = "0")
      StringTrimRight, num, num, 1
  }
Return num
}
bazkeys
Posts: 98
Joined: 20 Jan 2021, 21:58

Re: output only necessary decimal places

20 Sep 2021, 01:56

@Rohwedder

Not sure what you're asking/saying there. Guessing you're saying that that old post's code doesn't work well. I'm using your code anyway ;) And did some further stuff beyond what I originally intended, added increments of .025 in addition to .1, and so far working perfectly. Thank you once again :D
just me
Posts: 9442
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: output only necessary decimal places

20 Sep 2021, 04:59

Code: Select all

No := 3.1420000
MsgBox, % Format("{:0.15g}", No) ; 3.142
should be sufficient in the majority of cases.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], LepG, mapcarter, OrangeCat and 245 guests