diff --git a/priv/static/squared_away.mjs b/priv/static/squared_away.mjs index a3c0e53..de76dfe 100644 --- a/priv/static/squared_away.mjs +++ b/priv/static/squared_away.mjs @@ -238,6 +238,13 @@ function structurallyCompatibleObjects(a, b) { return false; return a.constructor === b.constructor; } +function remainderInt(a, b) { + if (b === 0) { + return 0; + } else { + return a % b; + } +} function divideInt(a, b) { return Math.trunc(divideFloat(a, b)); } @@ -4439,6 +4446,26 @@ var Rat = class extends CustomType { function from_int(input2) { return new Rat(from2(input2), from2(1)); } +function commas(n) { + let _pipe = n; + let _pipe$1 = reverse3(_pipe); + let _pipe$2 = graphemes(_pipe$1); + return index_fold( + _pipe$2, + "", + (acc, c, i) => { + let $ = i === 0; + let $1 = remainderInt(i, 3) === 0; + if (!$ && $1) { + return c + "," + acc; + } else if ($) { + return c + acc; + } else { + return c + acc; + } + } + ); +} function remove_zeroes_and_decimal(loop$txt) { while (true) { let txt = loop$txt; @@ -4477,7 +4504,10 @@ function do_to_string(loop$precision, loop$remainder, loop$d, loop$acc) { function to_string9(rat, precision) { let n = rat.numerator; let d = rat.denominator; - let whole = to_string8(divide(n, d)); + let whole = (() => { + let _pipe = to_string8(divide(n, d)); + return commas(_pipe); + })(); let decimal_part = modulo(n, d); let $ = isEqual(decimal_part, from2(0)); if ($) { diff --git a/src/squared_away/squared_away_lang/util/rational.gleam b/src/squared_away/squared_away_lang/util/rational.gleam index 5ba2d84..349b3f7 100644 --- a/src/squared_away/squared_away_lang/util/rational.gleam +++ b/src/squared_away/squared_away_lang/util/rational.gleam @@ -80,7 +80,7 @@ pub fn sum(rats: List(Rat)) -> Rat { pub fn to_string(rat: Rat, precision: Int) -> String { let Rat(n, d) = rat - let whole = bigi.to_string(bigi.divide(n, d)) + let whole = bigi.to_string(bigi.divide(n, d)) |> commas let decimal_part = bigi.modulo(n, d) case decimal_part == bigi.from_int(0) { True -> whole @@ -91,6 +91,15 @@ pub fn to_string(rat: Rat, precision: Int) -> String { } } +fn commas(n: String) -> String { + n |> string.reverse |> string.to_graphemes |> list.index_fold("", fn(acc, c, i) { + case i == 0, i % 3 == 0 { + False, True -> c <> "," <> acc + True, _ | _, False -> c <> acc + } + }) +} + fn remove_zeroes_and_decimal(txt: String) -> String { case txt { "0" <> rest -> remove_zeroes_and_decimal(rest)