Thousand's separator

Started by jopython, November 04, 2013, 01:16:34 PM

Previous topic - Next topic

jopython

Is there a newlisp built-in for a thousands separator? Say in python we can



In [5]: format(123456789,',d')
Out[5]: '123,456,789'

steloflute

#1
My attempt:



>
(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

#2
Another one.


(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.


> (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

#3
AFAIK, there is no built-in facility for what you want.  However, if Lutz opted to employ http://stackoverflow.com/questions/11694901/how-can-i-format-currency-with-commas-in-c/11695126#11695126">this idea here and give you an interface to it through `format`, e.g.


> (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

#4
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

#5
One-liner:



(define (format1000 n)
(append (sgn n "-" "" "") (reverse (join (explode (reverse (string (abs n))) 3) ","))))
(format1000 123456789)
(format1000 -123456789)

jopython

#6
Thank you everyone for your solutions.

rickyboy

#7
Quote from: "steloflute"One-liner:

(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

#8
Quote from: "rickyboy"
Quote from: "steloflute"One-liner:

(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

#9
Quote from: "fdb"



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"


(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\">//http://www.cngrayhat.org