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

Post your working scripts, libraries and tools for AHK v1.1 and older
Suresh
Posts: 35
Joined: 03 May 2016, 18:58

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

21 Jul 2017, 16:09

FanaticGuru wrote:It would seem that a simple Format will handle most of what is needed.
I need 0.000001 to be left as it is, but g option converts it to e notation

Code: Select all

MsgBox % Format("{1:g}", 0.000001 ) ; 1e-006
which is not even valid for AHK math operations. The result should be 1.0e-006 to be a valid number.
carno
Posts: 265
Joined: 20 Jun 2014, 16:48

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

29 Jan 2019, 11:58

How can this be modified so that I can trim the leading floating zeros only after the decimal point but leave 2 zeros after the decimal point if all digits after the decimal point are zeros and 1 zero after the first non-zero digit following the decimal point if the remaining digits are also zeros? Examples:
123.000000 > 123.00
123.012345 > 123.012345
123.100000 > 123.10
123.123456 > 123.123456
123.120000 > 123.12
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

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

29 Jan 2019, 13:40

Suresh wrote:
21 Jul 2017, 16:09
FanaticGuru wrote:It would seem that a simple Format will handle most of what is needed.
I need 0.000001 to be left as it is, but g option converts it to e notation

Code: Select all

MsgBox % Format("{1:g}", 0.000001 ) ; 1e-006
which is not even valid for AHK math operations. The result should be 1.0e-006 to be a valid number.
Old post but, you want f for plain old floating point.
MsgBox % Format("{:f}", 0.000001)

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

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

29 Jan 2019, 13:43

carno wrote:
29 Jan 2019, 11:58
How can this be modified so that I can trim the leading floating zeros only after the decimal point but leave 2 zeros after the decimal point if all digits after the decimal point are zeros and 1 zero after the first non-zero digit following the decimal point if the remaining digits are also zeros? Examples:
123.000000 > 123.00
123.012345 > 123.012345
123.100000 > 123.10
123.123456 > 123.123456
123.120000 > 123.12

I don't think Format can do this but here is a RegEx:

Code: Select all

for k, v in [123.000000, 123.012345, 123.100000, 123.123456, 123.120000]
	MsgBox % RemoveOver2TrailingZero(v)

RemoveOver2TrailingZero(x)
{
	return RegExReplace(x, "(^\d*.\d\d\d*?)(0*)$", "$1") 
}
Tested only on your example numbers.

FG
Last edited by FanaticGuru on 29 Jan 2019, 17:07, edited 1 time in total.
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

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

29 Jan 2019, 15:10

Note: carno has asked the same question here, which is a better place to answer:
RegEx to trim floating decimal places - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=61524
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: mikeyww and 191 guests