Formatting Numbers

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
BGM
Posts: 507
Joined: 20 Nov 2013, 20:56
Contact:

Formatting Numbers

22 Apr 2018, 18:11

Oh, I am completely bewildered by this.

In my script, when I do math with division, I get numbers in scientific format - with e's in them (I'm writing a calculator app)

610*2.17 = 1.3237000000000000e+003

My code is long and complicated and I'm not sure what I did to get this to happen.
What do I look for, and how do I get it to just give me 2 decimal points?

Now, I have this which I am using to switch back to regular numbers (because I switch to hex in another place)

Code: Select all

ResetNormalFormats:
	if(usedecimalplaces){
		normalfloatformat := "0." . decimalplaces+1 . "g"	;you have to add 1 to the number to display the correct amount of digits
	}else{
		normalfloatformat :=  "0." . maxdecimalplaces+1 . "g"
	}	
	
	;W := W="" ? "0.6g" : "0." . W 
	;SetFormat Integer, %FormI%
	SetFormat Integer, D	;decimal format
	SetFormat FLOAT, %normalfloatformat%
return
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Formatting Numbers

23 Apr 2018, 08:51

why are u appending a "g"?
User avatar
BGM
Posts: 507
Joined: 20 Nov 2013, 20:56
Contact:

Re: Formatting Numbers

23 Apr 2018, 09:26

At one time I thought it would help eliminate the trailing zeros...

So, I've removed the g and the +1 and now it's not doing exponents any more so it's not so horrendous.
Then I use rtrim() to remove trailing zeros.

Code: Select all

ResetNormalFormats(){
	global
	if(usedecimalplaces){
		normalfloatformat := "0." . decimalplaces
	}else{
		normalfloatformat :=  "0." . maxdecimalplaces
	}	
	
	SetFormat Integer, %FormI%
	SetFormat Integer, D	;decimal format
	SetFormat FLOAT, %normalfloatformat%
}
Then in my code, I do this:

Code: Select all

ResetNormalFormats()
y := rtrim(y,0)
User avatar
BGM
Posts: 507
Joined: 20 Nov 2013, 20:56
Contact:

Re: Formatting Numbers

23 Apr 2018, 10:00

So, with this format for the float, it always gives me trailing zeros, and the setformat is undoing my rtrim due to it's global nature and the different threads.
Also, you can use setformat to format both decimals AND floats simultaneously without having to know ahead of time what kind of value you have.

Is there a way to change the parameter for setformat FLOAT so that there are no trailing zeros after the decimal?
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Formatting Numbers

23 Apr 2018, 13:28

- SetFormat isn't available in AutoHotkey v2, so you could try getting used to the Format function instead. See FORMAT FUNCTION EXAMPLES, here:
jeeswg's characters tutorial - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=26486
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Formatting Numbers

23 Apr 2018, 14:10

Code: Select all

MsgBox, % formatFloatTrimZero(610 * 2.17)
MsgBox, % formatFloatTrimZero(610 * 2.171231234122342352623)
MsgBox, % formatFloatTrimZero(610 * 2.171231234122342352623, 10)
MsgBox, % formatFloatTrimZero(610 * 2)

formatFloatTrimZero(floatNum, decimalPlace := 6) {
	if floatNum is integer
		throw Exception("Illegal argument passed to function " . A_ThisFunc . "().`nSpecifically: Integer (" . floatNum . ")")

	return RTrim(Format("{:0." . decimalPlace . "f}", floatNum), "0")
}
User avatar
BGM
Posts: 507
Joined: 20 Nov 2013, 20:56
Contact:

Re: Formatting Numbers

26 Apr 2018, 17:01

Thanks y'all, but my problem is that SetFormat has a global effect; my script is a bit old (from before it was deprecated), and I'm not sure how to change things so that I could work on each variable. Maybe someone could take a look at my script and give me some advice?

I'll leave these links here for awhile; I don't really want them public, but it's okay for awhile, I think.

This is the math part:
https://gist.github.com/BGMcoder/2d6e1e ... 98a369714b

This is the part where the math function is called, so you can see how it is returned to the GUI.
https://gist.github.com/BGMcoder/f29b70 ... a2d569ed53

Problem is that SetFormat in the math part doesn't affect the settings in the GUI part.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Freddie, haomingchen1998, mmflume, scriptor2016, ShatterCoder and 92 guests