提交 01874ff1 编写于 作者: M mahaifeng

[number]去除手动生成的代码(添加示例代码)

上级 0b467edf
...@@ -5,9 +5,16 @@ export function testNumber() : Result { ...@@ -5,9 +5,16 @@ export function testNumber() : Result {
return describe("Number", () => { return describe("Number", () => {
test('toFixed', () => { test('toFixed', () => {
// #TEST Number.toFixed
function financial(x : Number) : String { function financial(x : Number) : String {
return x.toFixed(2); return x.toFixed(2);
} }
console.log(financial(123.456));
// expected output: "123.46"
console.log(financial(0.004));
// expected output: "0.00"
// #END
expect(financial(123.456)).toEqual('123.46'); expect(financial(123.456)).toEqual('123.46');
expect(financial(0.004)).toEqual("0.00"); expect(financial(0.004)).toEqual("0.00");
expect(financial(0)).toEqual("0.00"); expect(financial(0)).toEqual("0.00");
...@@ -74,7 +81,7 @@ export function testNumber() : Result { ...@@ -74,7 +81,7 @@ export function testNumber() : Result {
test('number-from-json-parse', () => { test('number-from-json-parse', () => {
type A = { type A = {
a:number a : number
} }
let aj = JSON.parse<A>('{"a":1}'); let aj = JSON.parse<A>('{"a":1}');
expect(aj?.a == 1).toEqual(true); expect(aj?.a == 1).toEqual(true);
...@@ -82,13 +89,13 @@ export function testNumber() : Result { ...@@ -82,13 +89,13 @@ export function testNumber() : Result {
expect(aj?.a == 1.0).toEqual(true); expect(aj?.a == 1.0).toEqual(true);
expect(aj?.a == 1.0 as number).toEqual(true); expect(aj?.a == 1.0 as number).toEqual(true);
// #ifdef APP-ANDROID // #ifdef APP-ANDROID
expect(numberEquals(aj?.a,1)).toEqual(true); expect(numberEquals(aj?.a, 1)).toEqual(true);
expect(numberEquals(aj?.a,1 as number)).toEqual(true); expect(numberEquals(aj?.a, 1 as number)).toEqual(true);
expect(numberEquals(aj?.a,1.0)).toEqual(true); expect(numberEquals(aj?.a, 1.0)).toEqual(true);
expect(numberEquals(aj?.a,1.0 as number)).toEqual(true); expect(numberEquals(aj?.a, 1.0 as number)).toEqual(true);
let ki:Int = 1; let ki : Int = 1;
let kd:Double = 1.0; let kd : Double = 1.0;
let kf:Float = (1.0).toFloat(); let kf : Float = (1.0).toFloat();
expect(aj?.a == ki).toEqual(true); expect(aj?.a == ki).toEqual(true);
expect(aj?.a == kd).toEqual(true); expect(aj?.a == kd).toEqual(true);
expect(aj?.a == kf).toEqual(true); expect(aj?.a == kf).toEqual(true);
...@@ -183,12 +190,19 @@ export function testNumber() : Result { ...@@ -183,12 +190,19 @@ export function testNumber() : Result {
}) })
test('toPrecision', () => { test('toPrecision', () => {
// #TEST Number.toPrecision
console.log(123.456.toPrecision(4))//123.5
// #END
expect(123.456.toPrecision(4)).toEqual("123.5"); expect(123.456.toPrecision(4)).toEqual("123.5");
expect(0.004.toPrecision(4)).toEqual("0.004000"); expect(0.004.toPrecision(4)).toEqual("0.004000");
// expect(1.23e5.toPrecision(4)).toEqual("1.230e+5"); // expect(1.23e5.toPrecision(4)).toEqual("1.230e+5");
}) })
test('toString', () => { test('toString', () => {
// #TEST Number.toString
console.log((10).toString())//10
// #END
expect((10).toString()).toEqual("10"); expect((10).toString()).toEqual("10");
expect((17).toString()).toEqual("17"); expect((17).toString()).toEqual("17");
expect((17.2).toString()).toEqual("17.2"); expect((17.2).toString()).toEqual("17.2");
...@@ -201,12 +215,12 @@ export function testNumber() : Result { ...@@ -201,12 +215,12 @@ export function testNumber() : Result {
expect((-0xff).toString(2)).toEqual("-11111111"); expect((-0xff).toString(2)).toEqual("-11111111");
const a = 1e38 const a = 1e38
expect(a.toString(16).substring(0,12)).toEqual("4b3b4ca85a86"); expect(a.toString(16).substring(0, 12)).toEqual("4b3b4ca85a86");
expect(a.toString(16).length).toEqual(32); expect(a.toString(16).length).toEqual(32);
expect(a.toString(16).substring(23)).toEqual("000000000"); expect(a.toString(16).substring(23)).toEqual("000000000");
expect(a.toString(2).length).toEqual(127); expect(a.toString(2).length).toEqual(127);
expect(a.toString(2).substring(0,32)).toEqual("10010110011101101001100101010000"); expect(a.toString(2).substring(0, 32)).toEqual("10010110011101101001100101010000");
expect(a.toString(2).substring(100)).toEqual("000000000000000000000000000"); expect(a.toString(2).substring(100)).toEqual("000000000000000000000000000");
const b = 1e22 const b = 1e22
const bStr12 = b.toString(12) const bStr12 = b.toString(12)
...@@ -215,7 +229,7 @@ export function testNumber() : Result { ...@@ -215,7 +229,7 @@ export function testNumber() : Result {
* 浏览器端:27373a86ba1a195400000 * 浏览器端:27373a86ba1a195400000
*/ */
expect(bStr12.length).toEqual(21); expect(bStr12.length).toEqual(21);
expect(bStr12.substring(0,14)).toEqual("27373a86ba1a19"); expect(bStr12.substring(0, 14)).toEqual("27373a86ba1a19");
// #ifdef APP-ANDROID // #ifdef APP-ANDROID
expect((new UTSNumber(2709954670497349.5)).toString()).toEqual("2709954670497349.5"); expect((new UTSNumber(2709954670497349.5)).toString()).toEqual("2709954670497349.5");
...@@ -228,6 +242,9 @@ export function testNumber() : Result { ...@@ -228,6 +242,9 @@ export function testNumber() : Result {
}) })
test('valueOf', () => { test('valueOf', () => {
// #TEST Number.valueOf
console.log((10).valueOf()) //10
// #END
expect((10).valueOf()).toEqual(10); expect((10).valueOf()).toEqual(10);
expect((-10.2).valueOf()).toEqual(-10.2); expect((-10.2).valueOf()).toEqual(-10.2);
expect((0xf).valueOf()).toEqual(15); expect((0xf).valueOf()).toEqual(15);
...@@ -241,6 +258,54 @@ export function testNumber() : Result { ...@@ -241,6 +258,54 @@ export function testNumber() : Result {
expect(num.toDouble()).toEqual(10.123); expect(num.toDouble()).toEqual(10.123);
}) })
// #endif // #endif
test('toInt', () => {
// #ifdef APP
// #TEST Number.toInt
let a = 12
console.log(a.toInt());
// expected output: 12
// Int最大值2147483647,溢出了
let b = 2147483648
// expected output: -2147483648
// #END
expect(a.toInt()).toEqual(12);
// #endif
})
test('toByte', () => {
// #ifdef APP
// #TEST Number.toByte
let a = 12
console.log(a.toByte());
// expected output: 12
// #END
expect(a.toInt()).toEqual(12);
// #endif
})
test('toLong', () => {
// #ifdef APP
// #TEST Number.toLong
let a = 12
console.log(a.toLong());
// expected output: 12
// #END
expect(a.toLong()).toEqual(12);
// #endif
})
test('from', () => {
// #ifdef APP
// #TEST Number.from
let a = 12
let b = Number.from(a)
console.log(b);
// expected output: 12
// #END
expect(b).toEqual(12);
// #endif
})
test('numberEquals', () => { test('numberEquals', () => {
let a1 = 10.123 let a1 = 10.123
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册