Skip to content

Commit

Permalink
Fix Number.prototype.toFixed()
Browse files Browse the repository at this point in the history
  • Loading branch information
HalidOdat committed Sep 20, 2023
1 parent 8a78061 commit 44df688
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
3 changes: 1 addition & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion boa_engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ num-bigint = { version = "0.4.4", features = ["serde"] }
num-integer = "0.1.45"
bitflags = "2.4.0"
indexmap = "2.0.0"
ryu-js = "0.2.2"
ryu-js = { git = "https://github.com/boa-dev/ryu-js.git", branch = "feature/to-fixed" }
chrono = { version = "0.4.31", default-features = false, features = ["clock", "std"] }
fast-float = "0.2.0"
once_cell = "1.18.0"
Expand Down
18 changes: 5 additions & 13 deletions boa_engine/src/builtins/number/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,20 +272,12 @@ impl Number {
.ok_or_else(|| {
JsNativeError::range()
.with_message("toFixed() digits argument must be between 0 and 100")
})? as usize;
})? as u8;

// 6. If x is not finite, return ! Number::toString(x).
if !this_num.is_finite() {
Ok(JsValue::new(Self::to_native_string(this_num)))
// 10. If x ≥ 10^21, then let m be ! ToString(𝔽(x)).
} else if this_num >= 1.0e21 {
Ok(JsValue::new(f64_to_exponential(this_num)))
} else {
// Get rid of the '-' sign for -0.0 because of 9. If x < 0, then set s to "-".
let this_num = if this_num == 0_f64 { 0_f64 } else { this_num };
let this_fixed_num = format!("{this_num:.precision$}");
Ok(JsValue::new(this_fixed_num))
}
let mut buffer = ryu_js::Buffer::new();
let string = buffer.format_to_fixed(this_num, precision);

Ok(string.into())
}

/// `Number.prototype.toLocaleString( [locales [, options]] )`
Expand Down
9 changes: 9 additions & 0 deletions boa_engine/src/builtins/number/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,3 +479,12 @@ fn issue_2717() {
TestAction::assert_eq("(0.23046743672210102).toString(36)", "0.8aoosla2phj"),
]);
}

// https://github.com/boa-dev/boa/issues/2609
#[test]
fn issue_2609() {
run_test_actions([
TestAction::assert_eq("(1.25).toFixed(1)", "1.3"),
TestAction::assert_eq("(1.35).toFixed(1)", "1.4"),
]);
}

0 comments on commit 44df688

Please sign in to comment.