Skip to content

Commit

Permalink
commas
Browse files Browse the repository at this point in the history
  • Loading branch information
bcpeinhardt committed Nov 28, 2024
1 parent a1e9506 commit 165d32c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
32 changes: 31 additions & 1 deletion priv/static/squared_away.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 ($) {
Expand Down
11 changes: 10 additions & 1 deletion src/squared_away/squared_away_lang/util/rational.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 165d32c

Please sign in to comment.