Page 1 of 1
Thousand's separator
Posted: Mon Nov 04, 2013 9:16 pm
by jopython
Is there a newlisp built-in for a thousands separator? Say in python we can
Code: Select all
In [5]: format(123456789,',d')
Out[5]: '123,456,789'
Re: Thousand's separator
Posted: Tue Nov 05, 2013 3:26 am
by steloflute
My attempt:
Code: Select all
>
(define (format1000 n)
(setq s (reverse (string n)))
(setq acc "")
(for (i 0 (dec (length s)))
(when (and (> i 0) (zero? (% i 3)) (number? (int (s i))))
(setq acc (append acc ",")))
(setq acc (append acc (s i))))
(reverse acc))
> (format1000 123456789)
"123,456,789"
> (format1000 -123456789)
"-123,456,789"
Re: Thousand's separator
Posted: Tue Nov 05, 2013 5:27 am
by rickyboy
Another one.
Code: Select all
(define (explode* xs (width 1) (bool nil) (acc '()))
"Works like `explode`, but operates from right to left."
(if (empty? xs)
acc
(< (length xs) width)
(if bool acc (cons xs acc))
(explode* (slice xs 0 (- width)) width bool
(cons (slice xs (- width) width) acc))))
(define (humanize-integer integr (sep ","))
(string (sgn integr "-" "" "")
(join (explode* (string (abs integr)) 3) sep)))
In action.
Code: Select all
> (humanize-integer 123456789)
(humanize-integer 123456789)
"123,456,789"
> (humanize-integer -123456789)
(humanize-integer -123456789)
"-123,456,789"
Re: Thousand's separator
Posted: Tue Nov 05, 2013 5:48 am
by rickyboy
AFAIK, there is no built-in facility for what you want. However, if Lutz opted to employ
this idea here and give you an interface to it through `format`, e.g.
Code: Select all
> (format "%'d" 123456789)
"123,456,789"
then you'd have the built-in facility you want/desire. Just a thought.
Re: Thousand's separator
Posted: Tue Nov 05, 2013 11:12 am
by Lutz
This GNU extension doesn't work for me on Mac OS X 10.8 (Mount Lion) and also not on Windows 7 using the same example from the StackOverflow link. It's documented in the GNU libc documentation. I imagine it only works on GNU compiled libc. Haven't checked on Linux yet.
But I will include recognition of the optional ' (tick) anyway in the next version. So it may work on some platforms.
PS: does work on UBUNTU Linux!
Re: Thousand's separator
Posted: Fri Nov 08, 2013 9:06 am
by steloflute
One-liner:
Code: Select all
(define (format1000 n)
(append (sgn n "-" "" "") (reverse (join (explode (reverse (string (abs n))) 3) ","))))
(format1000 123456789)
(format1000 -123456789)
Re: Thousand's separator
Posted: Fri Nov 08, 2013 1:36 pm
by jopython
Thank you everyone for your solutions.
Re: Thousand's separator
Posted: Fri Nov 08, 2013 2:56 pm
by rickyboy
steloflute wrote:One-liner:
Code: Select all
(define (format1000 n)
(append (sgn n "-" "" "") (reverse (join (explode (reverse (string (abs n))) 3) ","))))
(format1000 123456789)
(format1000 -123456789)
Excellent!
Re: Thousand's separator
Posted: Sat Nov 09, 2013 9:34 pm
by fdb
rickyboy wrote:steloflute wrote:One-liner:
Code: Select all
(define (format1000 n)
(append (sgn n "-" "" "") (reverse (join (explode (reverse (string (abs n))) 3) ","))))
(format1000 123456789)
(format1000 -123456789)
Excellent!
I'm new to new lisp (love it!) but this doesn't work for numbers with a decimal part.
My try:
(define (format1000 n)
(let (t (parse (string n) "."))
(append (reverse (join (explode (reverse (first t)) 3) ","))
(if (empty? (rest t))
""
(append "." (last t))))))
> (format1000 12131)
"12,131"
> (format1000 12131.23)
"12,131.23"
Re: Thousand's separator
Posted: Sun Feb 09, 2014 6:19 am
by winger
fdb wrote:
I'm new to new lisp (love it!) but this doesn't work for numbers with a decimal part.
My try:
(define (format1000 n)
(let (t (parse (string n) "."))
(append (reverse (join (explode (reverse (first t)) 3) ","))
(if (empty? (rest t))
""
(append "." (last t))))))
> (format1000 12131)
"12,131"
> (format1000 12131.23)
"12,131.23"
Code: Select all
(define (format1000 n)
(append (and (find {^(.*)(\..*)} (string (abs n)) 1) (append (sgn n "-" "" "") (reverse (join (explode (reverse $1) 3) ",")))) (or $2 "")))
> (format1000 12131.23)
"12,131.23"