notjack
2019-12-3 17:50:23

@badkins do you want that feature because you’re doing stuff with amounts of currency?


badkins
2019-12-3 22:11:02

@notjack yes, I need to display currency values


badkins
2019-12-3 22:13:14

Rails does it like this: def parts left, right = number.to_s.split('.') left.gsub!(DELIMITED_REGEX) do \|digit_to_delimit\| "#{digit_to_delimit}#{options[:delimiter]}" end [left, right].compact end and then joins again with a . I originally had something more functional, but likely very inefficient: (define (number->currency n) (define (commatize str) (~> str (string->list _) (reverse _) (chunk _ 3) (map list->string _) (string-join _ ",") (string->list _) (reverse _) (list->string _))) (let* ([ str (~r n #:precision (list '= 2)) ] [ pair (string-split str ".") ] [ whole (first pair) ] [ fractional (second pair) ]) (string-append "$" (commatize whole) "." fractional))) so I’ll probably use the regex replace approach


badkins
2019-12-3 22:13:35

… even though it feels dirty :slightly_smiling_face:


notjack
2019-12-3 22:56:59

@badkins Would it help if there was a money? type? Such a type could 1) provide formatting functions, 2) do exact decimal math instead of IEEE math, 3) have a nice printed representation at the REPL, 4) make it easier to avoid getting your units mixed up


badkins
2019-12-3 22:58:06

Possibly. It’s easy enough to just use an integer for cents, but the formatting is important. Racket’s handling of exact numbers helps.


soegaard2
2019-12-3 22:58:20

I think Neil has implemented fixed point numbers somewhere.


notjack
2019-12-3 22:59:37

there’s probably tons of localization stuff relevant to this too


badkins
2019-12-3 23:03:52

Yeah, that’s what the racket-locale was supposed to handle I think, but it dropped the ball on formatting, and shook my confidence in the code in general.


notjack
2019-12-3 23:05:09

Huh, never realized racket-locale existed before


badkins
2019-12-3 23:06:22

I’ll just work out a very efficient commatization, and that will probably be all I need. Rails had a money library, but in the end, it was just easier to use cents.


samdphillips
2019-12-3 23:16:10

There is also the cldr libraries which address some of the locale stuff, but I think it was mostly to serve gregors uses.


samdphillips
2019-12-3 23:16:32

So I don’t know if it fully handles number. It does ok with times.


badkins
2019-12-4 00:25:13

Has a bug, e.g. -123 => $-,123.00


badkins
2019-12-4 02:10:41

After trying a few implementations, benchmarking & fixing bugs, here’s what I came up with. The regex replace method turned out to be too slow.


badkins
2019-12-4 02:12:19

I’m really glad I added the random test because it immediately exposed a stupid bug involving embedded zeros.


badkins
2019-12-4 02:25:37

Takes about 4 μs, so fast enough for my purposes.