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 Oct 5, 2023
1 parent a51581b commit 07b8fe1
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 @@ -66,7 +66,7 @@ num-bigint = { workspace = true, features = ["serde"] }
num-integer = "0.1.45"
bitflags.workspace = true
indexmap = { workspace = true, features = ["std"] }
ryu-js = "0.2.2"
ryu-js = { git = "https://github.com/boa-dev/ryu-js.git", branch = "feature/to-fixed" }
chrono = { workspace = true, default-features = false, features = ["clock", "std"] }
fast-float.workspace = true
once_cell = { workspace = true, features = ["std"] }
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 @@ -277,20 +277,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_js_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(js_string!(this_fixed_num)))
}
let mut buffer = ryu_js::Buffer::new();
let string = buffer.format_to_fixed(this_num, precision);

Ok(js_string!(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 @@ -534,3 +534,12 @@ fn issue_2717() {
),
]);
}

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

0 comments on commit 07b8fe1

Please sign in to comment.