Skip to content

Commit

Permalink
divide money by int
Browse files Browse the repository at this point in the history
  • Loading branch information
bcpeinhardt committed Nov 29, 2024
1 parent cc8e12c commit 624b088
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
20 changes: 13 additions & 7 deletions priv/static/squared_away.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5458,7 +5458,7 @@ function value_to_string(fv) {
return "Test Passing";
} else {
let dollars = fv.cents;
let str = "$" + to_string9(dollars, 100, true);
let str = "$" + to_string9(dollars, 2, true);
let $ = split_once2(str, ".");
if (!$.isOk() && !$[0]) {
return str + ".00";
Expand Down Expand Up @@ -5756,6 +5756,12 @@ function interpret(loop$env, loop$expr) {
let d = lhs2.cents;
let p2 = rhs2.percent;
return new Ok(new Usd(divide2(d, p2)));
} else if (lhs2 instanceof Usd && op instanceof Divide && rhs2 instanceof Integer) {
let d = lhs2.cents;
let p2 = rhs2.n;
return new Ok(
new Usd(divide2(d, from_int(p2)))
);
} else if (lhs2 instanceof Usd && op instanceof Minimum && rhs2 instanceof Usd) {
let d = lhs2.cents;
let p2 = rhs2.cents;
Expand Down Expand Up @@ -5828,7 +5834,7 @@ function interpret(loop$env, loop$expr) {
throw makeError(
"let_assert",
"squared_away/squared_away_lang/interpreter",
225,
228,
"",
"Pattern match failed, no pattern matched the value.",
{ value: v }
Expand All @@ -5849,7 +5855,7 @@ function interpret(loop$env, loop$expr) {
throw makeError(
"let_assert",
"squared_away/squared_away_lang/interpreter",
233,
236,
"",
"Pattern match failed, no pattern matched the value.",
{ value: v }
Expand All @@ -5870,7 +5876,7 @@ function interpret(loop$env, loop$expr) {
throw makeError(
"let_assert",
"squared_away/squared_away_lang/interpreter",
241,
244,
"",
"Pattern match failed, no pattern matched the value.",
{ value: v }
Expand Down Expand Up @@ -5940,7 +5946,7 @@ function interpret(loop$env, loop$expr) {
throw makeError(
"let_assert",
"squared_away/squared_away_lang/interpreter",
282,
285,
"",
"Pattern match failed, no pattern matched the value.",
{ value: v }
Expand All @@ -5966,7 +5972,7 @@ function interpret(loop$env, loop$expr) {
throw makeError(
"let_assert",
"squared_away/squared_away_lang/interpreter",
293,
296,
"",
"Pattern match failed, no pattern matched the value.",
{ value: v }
Expand All @@ -5987,7 +5993,7 @@ function interpret(loop$env, loop$expr) {
throw makeError(
"let_assert",
"squared_away/squared_away_lang/interpreter",
303,
306,
"",
"Pattern match failed, no pattern matched the value.",
{ value: v }
Expand Down
3 changes: 3 additions & 0 deletions src/squared_away/squared_away_lang/interpreter.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ pub fn interpret(
value.Usd(d), expr.Divide, value.Percent(p) -> {
Ok(value.Usd(rational.divide(d, p)))
}
value.Usd(d), expr.Divide, value.Integer(p) -> {
Ok(value.Usd(rational.divide(d, rational.from_int(p))))
}
value.Usd(d), expr.Minimum, value.Usd(p) -> {
Ok(value.Usd(rational.min(d, p)))
}
Expand Down
2 changes: 1 addition & 1 deletion src/squared_away/squared_away_lang/interpreter/value.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub fn value_to_string(fv: Value) -> String {
TestFail -> "Test Failure"
TestPass -> "Test Passing"
Usd(dollars) -> {
let str = "$" <> rational.to_string(dollars, 100, True)
let str = "$" <> rational.to_string(dollars, 2, True)
case string.split_once(str, ".") {
Error(Nil) -> str <> ".00"
Ok(#(_, cents)) -> {
Expand Down

0 comments on commit 624b088

Please sign in to comment.