Number.uts 5.1 KB
Newer Older
Y
yurj26 已提交
1 2 3 4
import { describe, test, expect, Result } from './tests.uts'

export function testNumber(): Result {
    return describe("Number", () => {
杜庆泉's avatar
杜庆泉 已提交
5
		
Y
yurj26 已提交
6 7 8 9 10 11 12 13
        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
			
lizhongyi_'s avatar
lizhongyi_ 已提交
15
			let num1: number = -1.1 / 0.1
杜庆泉's avatar
杜庆泉 已提交
16 17 18 19 20 21 22 23
			let num2: number = -1.1 / 0.1
			let num3: number = -1.1 / -0.1
			console.warn(num1)
			console.warn(num2)
			console.warn(num3)
			let obj = {"id":"3be2c600-894c-4231-aa56-82fd989cc961","result":{"result":[num1, num2, num3]}}
			console.log(JSON.stringify(obj))
			
Y
yurj26 已提交
24
        })
杜庆泉's avatar
杜庆泉 已提交
25 26
		
		test('parseInt', () => {
lizhongyi_'s avatar
lizhongyi_ 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
			expect(parseInt("123.456")).toEqual(123);
			expect(parseInt("123")).toEqual(123);
			expect(parseInt("123", 10)).toEqual(123);
			expect(parseInt("   123", 10)).toEqual(123);
			expect(parseInt("077")).toEqual(77);
			expect(parseInt("1.9")).toEqual(1);
			expect(parseInt("ff", 16)).toEqual(255);
			expect(parseInt("0xFF", 0)).toEqual(255);
			expect(parseInt("0xFF", 16)).toEqual(255);
			expect(parseInt("0xFF")).toEqual(255);
			expect(parseInt("0xFF", 10)).toEqual(0);
			expect(parseInt("0xF", 16)).toEqual(15);
			expect(parseInt("F", 16)).toEqual(15);
			expect(parseInt("021", 8)).toEqual(17);
			expect(parseInt("015", 10)).toEqual(15);
			expect(parseInt("15,123", 10)).toEqual(15);
			expect(parseInt("FXX123.99", 16)).toEqual(15);
			expect(parseInt("1111", 2)).toEqual(15);
			expect(parseInt("15 * 3", 10)).toEqual(15);
			expect(parseInt("15e2", 10)).toEqual(15);
			expect(parseInt("15px", 10)).toEqual(15);
			expect(parseInt("12", 13)).toEqual(15);
			expect(parseInt("17", 8)).toEqual(15);
杜庆泉's avatar
杜庆泉 已提交
50 51 52
		})
		test('parseFloat', () => {
			expect(parseFloat("11.20")).toEqual(11.2);
lizhongyi_'s avatar
lizhongyi_ 已提交
53 54 55 56 57
			expect(parseFloat("3.14")).toEqual(3.14);
			expect(parseFloat("  3.14  ")).toEqual(3.14);
			expect(parseFloat("314e-2")).toEqual(3.14);
			expect(parseFloat("0.0314E+2")).toEqual(3.14);
			expect(parseFloat("3.14some non-digit characters")).toEqual(3.14);
杜庆泉's avatar
杜庆泉 已提交
58 59 60 61 62 63 64
		})
		
		test('isFinite', () => {
		    expect(isFinite(1000 / 1)).toEqual(true);
			expect(isFinite(910)).toEqual(true);
			expect(isFinite(0)).toEqual(true);
		})
杜庆泉's avatar
杜庆泉 已提交
65
		test('isInteger', () => {
lizhongyi_'s avatar
lizhongyi_ 已提交
66
		    expect(Number.isInteger(12)).toEqual(true);
杜庆泉's avatar
杜庆泉 已提交
67 68 69 70
			expect(Number.isInteger(12.01)).toEqual(false);
			expect(Number.isInteger(-213123112.01)).toEqual(false);
			expect(Number.isInteger(-213123112)).toEqual(true);
		})
杜庆泉's avatar
杜庆泉 已提交
71 72 73 74 75 76 77 78
		
		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_ 已提交
79
			// expect(1.23e5.toPrecision(4)).toEqual("1.230e+5");
杜庆泉's avatar
杜庆泉 已提交
80 81
		})
		
lizhongyi_'s avatar
lizhongyi_ 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
		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);
		})
		
杜庆泉's avatar
杜庆泉 已提交
98 99 100 101 102 103 104
		// 数据转换操作示例
		test('toX', () => {
			let num = 10.123
			expect(num.toInt()).toEqual(10);
			expect(num.toDouble()).toEqual(10.123);
		})
		
杜庆泉's avatar
杜庆泉 已提交
105 106 107
		test('numberEquals', () => {
			let a1 = 10.123
			let b1 = 10.12300
杜庆泉's avatar
杜庆泉 已提交
108
			let c1 = 10.12300002
杜庆泉's avatar
杜庆泉 已提交
109
			expect(a1 == b1).toEqual(true);
杜庆泉's avatar
杜庆泉 已提交
110 111 112 113
			expect(a1 === b1).toEqual(true);
			expect(a1 !== 10.123001).toEqual(true);
			expect(a1 != c1).toEqual(true);
			
杜庆泉's avatar
杜庆泉 已提交
114 115 116
			let a2 = 0
			let b2 = 0.0
			expect(a2 == b2).toEqual(true);
杜庆泉's avatar
杜庆泉 已提交
117
			expect(a2 === b2).toEqual(true);
杜庆泉's avatar
杜庆泉 已提交
118 119 120 121
			
			let a3 = -1000
			let b3 = -1000.0
			expect(a3 == b3).toEqual(true);
杜庆泉's avatar
杜庆泉 已提交
122
			expect(a3 === b3).toEqual(true);
杜庆泉's avatar
杜庆泉 已提交
123 124 125 126
			
			let a4 = 123456789123456
			let b4 = 123456789123456.0
			expect(a4 == b4).toEqual(true);
杜庆泉's avatar
杜庆泉 已提交
127 128
			expect(a4 === b4 * 1.0).toEqual(true);
			
杜庆泉's avatar
杜庆泉 已提交
129 130 131
			
			let a5 = -123456789123456
			let b5 = -123456789123456.01
杜庆泉's avatar
杜庆泉 已提交
132 133 134
			expect(a5 != b5).toEqual(true);
			expect(a5 !== b5).toEqual(true);
			
杜庆泉's avatar
杜庆泉 已提交
135 136 137 138 139
			
			let a6 = 246.0 / 123 * 1.0
			let b6 = 2
			let c6 = 2.0
			expect(a6 == b6).toEqual(true);
杜庆泉's avatar
杜庆泉 已提交
140 141 142
			expect(b6 === c6).toEqual(true);
			expect(b6 !== c6 * 1.001).toEqual(true);
			expect(b6 != c6 * 1.001).toEqual(true);
杜庆泉's avatar
杜庆泉 已提交
143 144 145 146
			
			let a7 = -10 * 123.2
			let b7 = - 1232
			expect(a7 == b7).toEqual(true);
杜庆泉's avatar
杜庆泉 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
			expect(a7 != b7 * -1).toEqual(true);
			
			let a8 = -101 * 123.201
			expect(a8 == -12443.301).toEqual(true);
			expect(a8 === -12443.301).toEqual(true);
			expect(a8 !== -12443.301 * 0.999).toEqual(true);
			expect(a8 != -12443.301 * 0.999999).toEqual(true);
			
			let a9 = -0.02 * -123456
			expect(a9 == 2469.12).toEqual(true);
			expect(a9 === 2469.12).toEqual(true);
			expect(a9 != 2469.1201).toEqual(true);
			
			let a10 = 123456789123456.123456789123456
			let b10 = 123456789123456.123456789123456000000
			expect(a10 == b10).toEqual(true);
			expect(a10 === b10).toEqual(true);
			
杜庆泉's avatar
杜庆泉 已提交
165 166 167 168
			
		})
		
		
Y
yurj26 已提交
169 170
    })
}