File tree Expand file tree Collapse file tree 2 files changed +38
-0
lines changed Expand file tree Collapse file tree 2 files changed +38
-0
lines changed Original file line number Diff line number Diff line change @@ -213,6 +213,13 @@ extern {
213
213
extern {
214
214
pub type Number ;
215
215
216
+ /// The toPrecision() method returns a string representing the Number
217
+ /// object to the specified precision.
218
+ ///
219
+ /// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toPrecision
220
+ #[ wasm_bindgen( catch, method, js_name = toPrecision) ]
221
+ pub fn to_precision ( this : & Number , precision : u8 ) -> Result < String , JsValue > ;
222
+
216
223
/// The toString() method returns a string representing the
217
224
/// specified Number object.
218
225
///
Original file line number Diff line number Diff line change 2
2
3
3
use super :: project;
4
4
5
+ #[ test]
6
+ fn to_precision ( ) {
7
+ project ( )
8
+ . file ( "src/lib.rs" , r#"
9
+ #![feature(proc_macro, wasm_custom_section)]
10
+
11
+ extern crate wasm_bindgen;
12
+ use wasm_bindgen::prelude::*;
13
+ use wasm_bindgen::js;
14
+
15
+ #[wasm_bindgen]
16
+ pub fn to_precision(this: &js::Number, precision: u8) -> String {
17
+ let result = this.to_precision(precision);
18
+ let result = match result {
19
+ Ok(num) => num,
20
+ Err(_err) => "RangeError".to_string()
21
+ };
22
+ result
23
+ }
24
+ "# )
25
+ . file ( "test.ts" , r#"
26
+ import * as assert from "assert";
27
+ import * as wasm from "./out";
28
+
29
+ export function test() {
30
+ assert.equal(wasm.to_precision(0.1, 3), "0.100");
31
+ assert.equal(wasm.to_precision(10, 101), "RangeError");
32
+ }
33
+ "# )
34
+ . test ( )
35
+ }
5
36
6
37
#[ test]
7
38
fn to_string ( ) {
You can’t perform that action at this time.
0 commit comments