Thousand's separator

Q&A's, tips, howto's
Locked
jopython
Posts: 123
Joined: Tue Sep 14, 2010 3:08 pm

Thousand's separator

Post 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'

steloflute
Posts: 13
Joined: Tue Nov 06, 2012 4:02 pm
Contact:

Re: Thousand's separator

Post 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"

rickyboy
Posts: 607
Joined: Fri Apr 08, 2005 7:13 pm
Location: Front Royal, Virginia

Re: Thousand's separator

Post 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"
(λx. x x) (λx. x x)

rickyboy
Posts: 607
Joined: Fri Apr 08, 2005 7:13 pm
Location: Front Royal, Virginia

Re: Thousand's separator

Post 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.
(λx. x x) (λx. x x)

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Re: Thousand's separator

Post 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!

steloflute
Posts: 13
Joined: Tue Nov 06, 2012 4:02 pm
Contact:

Re: Thousand's separator

Post 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)

jopython
Posts: 123
Joined: Tue Sep 14, 2010 3:08 pm

Re: Thousand's separator

Post by jopython »

Thank you everyone for your solutions.

rickyboy
Posts: 607
Joined: Fri Apr 08, 2005 7:13 pm
Location: Front Royal, Virginia

Re: Thousand's separator

Post 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!
(λx. x x) (λx. x x)

fdb
Posts: 66
Joined: Sat Nov 09, 2013 8:49 pm

Re: Thousand's separator

Post 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"

winger
Posts: 46
Joined: Wed Mar 14, 2012 7:31 am

Re: Thousand's separator

Post 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"
Welcome to a newlisper home:)
http://www.cngrayhat.org

Locked