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 May 8, 2023
1 parent 7605453 commit df9a016
Show file tree
Hide file tree
Showing 3 changed files with 7 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.3", features = ["serde"] }
num-integer = "0.1.45"
bitflags = "2.2.1"
indexmap = "1.9.3"
ryu-js = "0.2.2"
ryu-js = { git = "https://github.com/boa-dev/ryu-js.git", branch = "feature/to-fixed" }
chrono = { version = "0.4.24", default-features = false, features = ["clock", "std"] }
fast-float = "0.2.0"
once_cell = "1.17.1"
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

0 comments on commit df9a016

Please sign in to comment.