Skip to content

Commit 7936e1e

Browse files
authored
Merge pull request #305 from sendilkumarn/number-fixed-exponential
Adds Number to_fixed and to_exponential function
2 parents 243f73e + 32bc9f2 commit 7936e1e

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
@@ -244,6 +244,20 @@ extern {
244244
#[wasm_bindgen(catch, method, js_name = toPrecision)]
245245
pub fn to_precision(this: &Number, precision: u8) -> Result<JsString, JsValue>;
246246

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

tests/all/js_globals/Number.rs

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

0 commit comments

Comments
 (0)