Number.uts 1.6 KB
Newer Older
Y
yurj26 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
import { describe, test, expect, Result } from './tests.uts'

export function testNumber(): Result {
    return describe("Number", () => {
        test('toFixed', () => {
            function financial(x: Number): String {
              return x.toFixed(2);
            }
            expect(financial(123.456)).toEqual('123.46');
            expect(financial(0.004)).toEqual("0.00");
            expect(financial(0)).toEqual("0.00");
            expect(financial(1)).toEqual("1.00");
        })
杜庆泉's avatar
杜庆泉 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
		
		test('parseInt', () => {
		    expect(parseInt("123.456")).toEqual(123);
		})
		test('parseFloat', () => {
			expect(parseFloat("11.20")).toEqual(11.2);
		})
		
		test('isFinite', () => {
		    expect(isFinite(1000 / 1)).toEqual(true);
			expect(isFinite(910)).toEqual(true);
			expect(isFinite(0)).toEqual(true);
		})
		
		test('isNaN', () => {
			expect(isNaN(0)).toEqual(false);
		})
		
		test('toPrecision', () => {
			expect(123.456.toPrecision(4)).toEqual("123.5");
			expect(0.004.toPrecision(4)).toEqual("0.004000");
lizhongyi_'s avatar
lizhongyi_ 已提交
35
			// expect(1.23e5.toPrecision(4)).toEqual("1.230e+5");
杜庆泉's avatar
杜庆泉 已提交
36 37
		})
		
lizhongyi_'s avatar
lizhongyi_ 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
		test('toString', () => {
			expect((10).toString()).toEqual("10");
			expect((17).toString()).toEqual("17");
			expect((17.2).toString()).toEqual("17.2");
			expect((6).toString(2)).toEqual("110");
			expect((254).toString(16)).toEqual("fe");
			expect((-10).toString(2)).toEqual("-1010");
			expect((-0xff).toString(2)).toEqual("-11111111");
		})
		
		test('valueOf', () => {
			expect((10).valueOf()).toEqual(10);
			expect((-10.2).valueOf()).toEqual(-10.2);
			expect((0xf).valueOf()).toEqual(15);
		})
		
Y
yurj26 已提交
54 55
    })
}