Skip to content

Commit 0f57398

Browse files
committed
Add to_fixed and to_exponential function
1 parent 4c7b267 commit 0f57398

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

src/js.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,20 @@ extern {
246246
#[wasm_bindgen(catch, method, js_name = toPrecision)]
247247
pub fn to_precision(this: &Number, precision: u8) -> Result<String, JsValue>;
248248

249+
/// The toFixed() method returns a string representing the Number
250+
/// object using fixed-point notation.
251+
///
252+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed
253+
#[wasm_bindgen(catch, method, js_name = toFixed)]
254+
pub fn to_fixed(this: &Number, digits: u8) -> Result<String, JsValue>;
255+
256+
/// The toExponential() method returns a string representing the Number
257+
/// object in exponential notation.
258+
///
259+
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed
260+
#[wasm_bindgen(catch, method, js_name = toExponential)]
261+
pub fn to_exponential(this: &Number, fraction_digits: u8) -> Result<String, JsValue>;
262+
249263
/// The toString() method returns a string representing the
250264
/// specified Number object.
251265
///

tests/all/js_globals/Number.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,67 @@ fn value_of() {
125125
"#)
126126
.test()
127127
}
128+
129+
#[test]
130+
fn to_fixed() {
131+
project()
132+
.file("src/lib.rs", r#"
133+
#![feature(proc_macro, wasm_custom_section)]
134+
135+
extern crate wasm_bindgen;
136+
use wasm_bindgen::prelude::*;
137+
use wasm_bindgen::js;
138+
139+
#[wasm_bindgen]
140+
pub fn to_fixed(this: &js::Number, digits: u8) -> String {
141+
let result = this.to_fixed(digits);
142+
let result = match result {
143+
Ok(num) => num,
144+
Err(_err) => "RangeError".to_string()
145+
};
146+
result
147+
}
148+
"#)
149+
.file("test.ts", r#"
150+
import * as assert from "assert";
151+
import * as wasm from "./out";
152+
153+
export function test() {
154+
assert.equal(wasm.to_fixed(123.456, 2), "123.46");
155+
assert.equal(wasm.to_fixed(10, 101), "RangeError");
156+
}
157+
"#)
158+
.test()
159+
}
160+
161+
#[test]
162+
fn to_exponential() {
163+
project()
164+
.file("src/lib.rs", r#"
165+
#![feature(proc_macro, wasm_custom_section)]
166+
167+
extern crate wasm_bindgen;
168+
use wasm_bindgen::prelude::*;
169+
use wasm_bindgen::js;
170+
171+
#[wasm_bindgen]
172+
pub fn to_exponential(this: &js::Number, fraction_digits: u8) -> String {
173+
let result = this.to_exponential(fraction_digits);
174+
let result = match result {
175+
Ok(num) => num,
176+
Err(_err) => "RangeError".to_string()
177+
};
178+
result
179+
}
180+
"#)
181+
.file("test.ts", r#"
182+
import * as assert from "assert";
183+
import * as wasm from "./out";
184+
185+
export function test() {
186+
assert.equal(wasm.to_exponential(123456, 2), "1.23e+5");
187+
assert.equal(wasm.to_exponential(10, 101), "RangeError");
188+
}
189+
"#)
190+
.test()
191+
}

0 commit comments

Comments
 (0)