diff --git a/src/js.rs b/src/js.rs index 3d4414aa..4acf60f0 100644 --- a/src/js.rs +++ b/src/js.rs @@ -213,6 +213,13 @@ extern { extern { pub type Number; + /// The toPrecision() method returns a string representing the Number + /// object to the specified precision. + /// + /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toPrecision + #[wasm_bindgen(catch, method, js_name = toPrecision)] + pub fn to_precision(this: &Number, precision: u8) -> Result; + /// The toString() method returns a string representing the /// specified Number object. /// diff --git a/tests/all/js_globals/Number.rs b/tests/all/js_globals/Number.rs index 96fe745a..fa215fc6 100644 --- a/tests/all/js_globals/Number.rs +++ b/tests/all/js_globals/Number.rs @@ -2,6 +2,37 @@ use super::project; +#[test] +fn to_precision() { + project() + .file("src/lib.rs", r#" + #![feature(proc_macro, wasm_custom_section)] + + extern crate wasm_bindgen; + use wasm_bindgen::prelude::*; + use wasm_bindgen::js; + + #[wasm_bindgen] + pub fn to_precision(this: &js::Number, precision: u8) -> String { + let result = this.to_precision(precision); + let result = match result { + Ok(num) => num, + Err(_err) => "RangeError".to_string() + }; + result + } + "#) + .file("test.ts", r#" + import * as assert from "assert"; + import * as wasm from "./out"; + + export function test() { + assert.equal(wasm.to_precision(0.1, 3), "0.100"); + assert.equal(wasm.to_precision(10, 101), "RangeError"); + } + "#) + .test() +} #[test] fn to_string() {