Thousand's separator

Q&A's, tips, howto's

Thousand's separator

Postby jopython » Mon Nov 04, 2013 9:16 pm

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'
jopython
 
Posts: 123
Joined: Tue Sep 14, 2010 3:08 pm

Re: Thousand's separator

Postby steloflute » Tue Nov 05, 2013 3:26 am

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"
steloflute
 
Posts: 13
Joined: Tue Nov 06, 2012 4:02 pm

Re: Thousand's separator

Postby rickyboy » Tue Nov 05, 2013 5:27 am

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: 594
Joined: Fri Apr 08, 2005 7:13 pm
Location: Front Royal, Virginia

Re: Thousand's separator

Postby rickyboy » Tue Nov 05, 2013 5:48 am

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)
rickyboy
 
Posts: 594
Joined: Fri Apr 08, 2005 7:13 pm
Location: Front Royal, Virginia

Re: Thousand's separator

Postby Lutz » Tue Nov 05, 2013 11:12 am

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!
Lutz
 
Posts: 5279
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California

Re: Thousand's separator

Postby steloflute » Fri Nov 08, 2013 9:06 am

One-liner:

Code: Select all
(define (format1000 n)
   (append (sgn n "-" "" "") (reverse (join (explode (reverse (string (abs n))) 3) ","))))
(format1000 123456789)
(format1000 -123456789)
steloflute
 
Posts: 13
Joined: Tue Nov 06, 2012 4:02 pm

Re: Thousand's separator

Postby jopython » Fri Nov 08, 2013 1:36 pm

Thank you everyone for your solutions.
jopython
 
Posts: 123
Joined: Tue Sep 14, 2010 3:08 pm

Re: Thousand's separator

Postby rickyboy » Fri Nov 08, 2013 2:56 pm

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)
rickyboy
 
Posts: 594
Joined: Fri Apr 08, 2005 7:13 pm
Location: Front Royal, Virginia

Re: Thousand's separator

Postby fdb » Sat Nov 09, 2013 9:34 pm

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"
fdb
 
Posts: 47
Joined: Sat Nov 09, 2013 8:49 pm

Re: Thousand's separator

Postby winger » Sun Feb 09, 2014 6:19 am

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
winger
 
Posts: 46
Joined: Wed Mar 14, 2012 7:31 am


Return to newLISP in the real world

Who is online

Users browsing this forum: No registered users and 2 guests

cron