S1000() : Separate 1000's in 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

S1000() : Separate 1000's in Number

17 Jul 2021, 16:00

Code: Select all

S1000(N) { ; Separate 1000's in N,       by SKAN on D47G/D47I @ tiny.cc/s1000
Return ( N := StrSplit((N<0 ? "-." : ".") . N, ".") ).Count() ? N.1
       . Trim( Format("{}{},{}{}{},{}{}{},{}{}{},{}{}{},{}{}{},{}{}{}." . N.3
       , StrSplit( Format("{:20}", Format("{:d}",N.2)))* ), " .,-" ) : 0
}

S1000_DE(N) { ; Separate 1000's in N,    by SKAN on D47G/D47I @ tiny.cc/s1000
Return ( N := StrSplit((N<0 ? "-," : ",") . StrReplace(N, ".", ","), ",") ).Count() ? N.1
       . Trim( Format("{}{}.{}{}{}.{}{}{}.{}{}{}.{}{}{}.{}{}{}.{}{}{}," . N.3
       , StrSplit( Format("{:20}", Format("{:d}",N.2)))* ), " .,-" ) : 0
}

S1000_IN(N) { ; Separate 1000's in N,    by SKAN on D47G/D47I @ tiny.cc/s1000
Return ( N := StrSplit((N<0 ? "-." : ".") . N, ".") ).Count() ? N.1
       . Trim( Format("{},{}{},{}{},{}{},{}{},{}{},{}{},{}{},{}{},{}{}{}." . N.3
       , StrSplit( Format("{:20}", Format("{:d}",N.2)))* ), " .,-" ) : 0
}

#NoEnv
#Warn
#SingleInstance, Force

MsgBox % S1000(-0)
MsgBox % S1000(-.5)
MsgBox % S1000(1000)
MsgBox % S1000(-123456.789)
MsgBox % S1000(2**63-1)
My Scripts and Functions: V1  V2
mcl
Posts: 361
Joined: 04 May 2018, 16:35

Re: S1000() : Separate 1000's in Number

17 Jul 2021, 21:41

Code: Select all

MsgBox % S1000(-0.5)

S1001(n) {
	Return RegexReplace(N, "(?<!-)(?<!^)(?=(\d{3})+(\.|$))", ",",, (N~="\.") ? ((N~="\.")-(N~="-")-2)/3 : -1 )
}
Johnny R
Posts: 348
Joined: 03 Oct 2013, 02:07

Re: S1000() : Separate 1000's in Number

18 Jul 2021, 00:11

It would be nice if the German format ("-12.345,67") could be taken into the script. Thanks.
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: S1000() : Separate 1000's in Number

18 Jul 2021, 02:49

mcl wrote:
17 Jul 2021, 21:41

Code: Select all

MsgBox % S1000(-0.5)
 
Thanks. Fixed. :thumbup:
 
Johnny R wrote:
18 Jul 2021, 00:11
It would be nice if the German format ("-12.345,67") could be taken into the script. Thanks.
 
I've added S1000_DE(). Please test it. :)
Johnny R
Posts: 348
Joined: 03 Oct 2013, 02:07

Re: S1000() : Separate 1000's in Number

18 Jul 2021, 04:33

Hallo SKAN, i've tested the script. MsgBox % S1000_DE(-123456.789) ignores the part ".789".
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: S1000() : Separate 1000's in Number

18 Jul 2021, 05:09

Johnny R wrote:
18 Jul 2021, 04:33
Hallo SKAN, i've tested the script. MsgBox % S1000_DE(-123456.789) ignores the part ".789".
Code modified. Please try again.
Johnny R
Posts: 348
Joined: 03 Oct 2013, 02:07

Re: S1000() : Separate 1000's in Number

18 Jul 2021, 05:19

Hallo Skan, now it works. Thank you! What means the function S1000_IN(N)?
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: S1000() : Separate 1000's in Number

18 Jul 2021, 05:28

Johnny R wrote:
18 Jul 2021, 05:19
What means the function S1000_IN(N)?
S1000_DE is German, S1000_IN is Indian.
See Indian numbering System
ozzii
Posts: 486
Joined: 30 Oct 2013, 06:04

Re: S1000() : Separate 1000's in Number

18 Jul 2021, 06:01

S1000_DE is German, S1000_IN is Indian.
It will not be more universal to add as options the thousands delimiter and decimals delimiter?
For example:
MsgBox % S1000_DE(-123456.789," ",",") -> -123 456,789
MsgBox % S1000_DE(-123456.789," ",".") -> -123 456.789
MsgBox % S1000_DE(-123456.789,".",",") -> -123.456,789

At least you will catch the majority and you can keep the exceptions like Indian in a function.

What do you think about that SKAN?
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: S1000() : Separate 1000's in Number

18 Jul 2021, 07:01

Hi @ozzii
This topic isn't really about formatting numbers. It merely demonstrates how to to do it abusing Format() command.
What you suggest is good and can be done... but these functions are already slower than @mcl's RegEx version.
In my current project, I'm calling this function only once and only for formatting bytes.

The recommended way for number formatting is using GetNumberFormat() ( my old code).
You can find a better/modern versions by @jNizM here.
ozzii
Posts: 486
Joined: 30 Oct 2013, 06:04

Re: S1000() : Separate 1000's in Number

19 Jul 2021, 04:49

Thank you SKAN for the answer.
I will look into the other scripts

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: Gewerd_Strauss and 228 guests