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

3
//@UTSAndroid.Suppress("IMPLICIT_BOXING_IN_IDENTITY_EQUALS")
4 5 6 7
export function testNumber() : Result {
  return describe("Number", () => {

    test('toFixed', () => {
8
      // #TEST Number.toFixed
9 10 11
      function financial(x : Number) : String {
        return x.toFixed(2);
      }
12 13 14 15 16 17
      console.log(financial(123.456));
      // expected output: "123.46"
      console.log(financial(0.004));
      // expected output: "0.00"
      // #END

18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
      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");

      let num1 : number = -1.1 / 0.1
      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))



      expect(123456.789.toFixed(2)).toEqual("123456.79");
      expect(12345600123.789123.toFixed(2)).toEqual("12345600123.79");
      expect((-123456.789).toFixed(5)).toEqual("-123456.78900");
      expect(parseFloat("16688995566.369").toFixed(3)).toEqual("16688995566.369");
雪洛's avatar
雪洛 已提交
38
      // #ifdef APP-ANDROID
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
      // android 专有数据类型
      let a1 = 56
      let a2 = -122
      expect(a1.toByte().toFixed(2)).toEqual("56.00");
      expect(a2.toByte().toFixed(2)).toEqual("-122.00");

      expect(a1.toShort().toFixed(5)).toEqual("56.00000");
      expect(a2.toShort().toFixed(5)).toEqual("-122.00000");
      // #endif
      /**
       * add since 2023-11-21
       * 发现部分场景在android 6 以下与web有差异,新增以下示例
       */
      let r = 2.449999999999999999;
      let t1 = r.toFixed(1);
      expect(t1).toEqual("2.5");

      let r2 = 2.4499999999999999;
      let t2 = r2.toFixed(1);
      expect(t2).toEqual("2.4");

      let r3 = -9007199254740991;
      let t3 = r3.toFixed(15);
      expect(t3).toEqual("-9007199254740991.000000000000000");

    })

    test('isFinite', () => {
      expect(isFinite(1000 / 1)).toEqual(true);
      expect(isFinite(910)).toEqual(true);
      expect(isFinite(0)).toEqual(true);
70
      // #ifdef APP-ANDROID
杜庆泉's avatar
杜庆泉 已提交
71
      let aj2 = JSON.parse('{"a":1}') as UTSJSONObject;
72
      let aNumber = aj2['a'] as Number
杜庆泉's avatar
杜庆泉 已提交
73
      expect(isFinite(aNumber)).toEqual(true);
74 75 76
      expect(Number.isFinite(aNumber)).toEqual(true);
      expect(Number.isFinite(11.00)).toEqual(true);
      expect(Number.isFinite(Double.POSITIVE_INFINITY)).toEqual(false);
lizhongyi_'s avatar
lizhongyi_ 已提交
77
      // #endif
78
    })
79 80 81



杜庆泉's avatar
杜庆泉 已提交
82 83
    test('number-from-json-parse', () => {
      type A = {
84
        a : number
杜庆泉's avatar
杜庆泉 已提交
85 86 87 88 89 90
      }
      let aj = JSON.parse<A>('{"a":1}');
      expect(aj?.a == 1).toEqual(true);
      expect(aj?.a == 1 as number).toEqual(true);
      expect(aj?.a == 1.0).toEqual(true);
      expect(aj?.a == 1.0 as number).toEqual(true);
lizhongyi_'s avatar
lizhongyi_ 已提交
91
      // #ifdef APP-ANDROID
92 93 94 95 96 97 98
      expect(numberEquals(aj?.a, 1)).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 as number)).toEqual(true);
      let ki : Int = 1;
      let kd : Double = 1.0;
      let kf : Float = (1.0).toFloat();
杜庆泉's avatar
杜庆泉 已提交
99 100 101
      expect(aj?.a == ki).toEqual(true);
      expect(aj?.a == kd).toEqual(true);
      expect(aj?.a == kf).toEqual(true);
杜庆泉's avatar
杜庆泉 已提交
102 103 104
      expect(aj?.a === ki).toEqual(true);
      expect(aj?.a === kd).toEqual(true);
      expect(aj?.a === kf).toEqual(true);
杜庆泉's avatar
杜庆泉 已提交
105 106 107
      // 对比JSON.parse 和 new 得到的对象属性
      let newA = A(1)
      expect(aj?.a == newA.a).toEqual(true);
杜庆泉's avatar
杜庆泉 已提交
108
      expect(aj?.a === newA.a).toEqual(true);
杜庆泉's avatar
杜庆泉 已提交
109 110
      newA.a = 1.0
      expect(aj?.a == newA.a).toEqual(true);
杜庆泉's avatar
杜庆泉 已提交
111
      expect(aj?.a === newA.a).toEqual(true);
杜庆泉's avatar
杜庆泉 已提交
112 113
      newA.a = 1.1
      expect(aj?.a == newA.a).toEqual(false);
杜庆泉's avatar
杜庆泉 已提交
114
      expect(aj?.a === newA.a).toEqual(false);
115

116 117 118 119 120 121 122
      let a10 = JSON.parse("10")
      let b10 = JSON.parse("10")
      let c10 = JSON.parse("10.0")
      expect(a10 == b10).toEqual(true);
      expect(a10 == c10).toEqual(true);
      let aj2 = JSON.parse<A>('{"a":1}');
      expect(aj?.a == aj2?.a).toEqual(true);
123

lizhongyi_'s avatar
lizhongyi_ 已提交
124
      // #endif
125

杜庆泉's avatar
杜庆泉 已提交
126
    })
127 128


129 130 131 132 133 134
    test('isInteger', () => {
      expect(Number.isInteger(12)).toEqual(true);
      expect(Number.isInteger(12.01)).toEqual(false);
      expect(Number.isInteger(-213123112.01)).toEqual(false);
      expect(Number.isInteger(-213123112)).toEqual(true);
    })
135

136
    test('Unary-Operators', () => {
杜庆泉's avatar
杜庆泉 已提交
137
      // #ifdef APP-ANDROID
138 139
      let a = JSON.parse("1") as Number
      a++;
140 141
      expect(JSON.stringify(a)).toEqual("2");
      expect(UTSAndroid.getJavaClass(a).name).toEqual("java.lang.Double");
142

143
      a--;
144 145
      expect(JSON.stringify(a)).toEqual("1");
      expect(UTSAndroid.getJavaClass(a).name).toEqual("java.lang.Double");
146
      a /= 0.5;
147 148
      expect(JSON.stringify(a)).toEqual("2");
      expect(UTSAndroid.getJavaClass(a).name).toEqual("java.lang.Double");
149
      a *= 5;
150 151
      expect(JSON.stringify(a)).toEqual("10");
      expect(UTSAndroid.getJavaClass(a).name).toEqual("java.lang.Double");
152
      a += -1;
153 154
      expect(JSON.stringify(a)).toEqual("9");
      expect(UTSAndroid.getJavaClass(a).name).toEqual("java.lang.Double");
155
      a -= -2;
156 157
      expect(JSON.stringify(a)).toEqual("11");
      expect(UTSAndroid.getJavaClass(a).name).toEqual("java.lang.Double");
158
      let a2 = a % 5
159 160
      expect(JSON.stringify(a2)).toEqual("1");
      expect(UTSAndroid.getJavaClass(a2).name).toEqual("java.lang.Double");
161

杜庆泉's avatar
杜庆泉 已提交
162 163
      let b = 12
      let c1 = b + a2
164 165
      expect(JSON.stringify(c1)).toEqual("13");
      expect(UTSAndroid.getJavaClass(c1).name).toEqual("java.lang.Double");
166

杜庆泉's avatar
杜庆泉 已提交
167
      let c2 = a2 + 2
168 169
      expect(JSON.stringify(c2)).toEqual("3");
      expect(UTSAndroid.getJavaClass(c2).name).toEqual("java.lang.Double");
170

杜庆泉's avatar
杜庆泉 已提交
171
      let c3 = 2 + a2
172 173
      expect(JSON.stringify(c3)).toEqual("3");
      expect(UTSAndroid.getJavaClass(c3).name).toEqual("java.lang.Double");
杜庆泉's avatar
杜庆泉 已提交
174
      // #endif
175 176


177
    })
178 179 180

    test('isNaN', () => {
      expect(isNaN(0)).toEqual(false);
lizhongyi_'s avatar
lizhongyi_ 已提交
181
      // #ifdef APP-ANDROID
杜庆泉's avatar
杜庆泉 已提交
182
      let aj2 = JSON.parse('{"a":1}') as UTSJSONObject;
183
      let aNumber = aj2['a'] as Number
杜庆泉's avatar
杜庆泉 已提交
184
      expect(isNaN(aNumber)).toEqual(false);
185 186 187 188
      expect(Number.isNaN(aNumber)).toEqual(false);
      expect(Number.isNaN(11)).toEqual(false);
      expect(Number.isNaN(null)).toEqual(false);
      expect(Number.isNaN(1 / 0)).toEqual(false);
lizhongyi_'s avatar
lizhongyi_ 已提交
189
      // #endif
190 191 192
    })

    test('toPrecision', () => {
193 194 195
      // #TEST Number.toPrecision
      console.log(123.456.toPrecision(4))//123.5
      // #END
196 197 198 199 200 201
      expect(123.456.toPrecision(4)).toEqual("123.5");
      expect(0.004.toPrecision(4)).toEqual("0.004000");
      // expect(1.23e5.toPrecision(4)).toEqual("1.230e+5");
    })

    test('toString', () => {
202 203 204 205
      // #TEST Number.toString
      console.log((10).toString())//10
      // #END

206 207 208 209 210 211
      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");
杜庆泉's avatar
杜庆泉 已提交
212 213 214
      expect((10.22).toString(8)).toEqual("12.16050753412172704");
      expect((-10.22).toString(8)).toEqual("-12.16050753412172704");
      expect((123456789987654).toString(16)).toEqual("7048861cc146");
215
      expect((-0xff).toString(2)).toEqual("-11111111");
lizhongyi_'s avatar
lizhongyi_ 已提交
216
      const a = 1e38
217 218

      expect(a.toString(16).substring(0, 12)).toEqual("4b3b4ca85a86");
杜庆泉's avatar
杜庆泉 已提交
219 220
      expect(a.toString(16).length).toEqual(32);
      expect(a.toString(16).substring(23)).toEqual("000000000");
221

杜庆泉's avatar
杜庆泉 已提交
222
      expect(a.toString(2).length).toEqual(127);
223
      expect(a.toString(2).substring(0, 32)).toEqual("10010110011101101001100101010000");
杜庆泉's avatar
杜庆泉 已提交
224
      expect(a.toString(2).substring(100)).toEqual("000000000000000000000000000");
lizhongyi_'s avatar
lizhongyi_ 已提交
225
      const b = 1e22
226 227 228 229 230 231
      const bStr12 = b.toString(12)
      /**
       * 移动端: 27373a86ba1a194a65054
       * 浏览器端:27373a86ba1a195400000
       */
      expect(bStr12.length).toEqual(21);
232
      expect(bStr12.substring(0, 14)).toEqual("27373a86ba1a19");
233
    
杜庆泉's avatar
杜庆泉 已提交
234
      // #ifdef APP-ANDROID
235
      expect(0.6.toString()).toEqual("0.6");
236
      expect((2709954670497349.5).toString(10)).toEqual("2709954670497349.5");
237 238 239 240 241 242
      // todo change JSON.stringify to toString
      expect((JSON.stringify(0.00000000000001))).toEqual("1e-14");
      expect((JSON.stringify(0.000001))).toEqual("0.000001");
      expect((JSON.stringify(0.0000001))).toEqual("1e-7");
      expect((JSON.stringify(100000000000000000000.0))).toEqual("100000000000000000000");
      expect((JSON.stringify(1000000000000000000000.0))).toEqual("1e+21");
杜庆泉's avatar
杜庆泉 已提交
243
      // #endif
244 245 246
    })

    test('valueOf', () => {
247 248 249
      // #TEST Number.valueOf
      console.log((10).valueOf()) //10
      // #END
250 251 252
      expect((10).valueOf()).toEqual(10);
      expect((-10.2).valueOf()).toEqual(-10.2);
      expect((0xf).valueOf()).toEqual(15);
Y
yurj26 已提交
253
    })
254

雪洛's avatar
雪洛 已提交
255
    // #ifdef APP-ANDROID
256 257 258 259 260 261
    // 数据转换操作示例
    test('toX', () => {
      let num = 10.123
      expect(num.toInt()).toEqual(10);
      expect(num.toDouble()).toEqual(10.123);
    })
雪洛's avatar
雪洛 已提交
262
    // #endif
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
    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
282
      // #ifdef APP-ANDROID
283
      console.log(a.toByte());
284
      // #endif
285 286 287 288 289 290 291
      // expected output: 12
      // #END
      expect(a.toInt()).toEqual(12);
      // #endif
    })

    test('toLong', () => {
292
      // #ifdef APP-ANDROID
293 294 295 296 297 298 299 300 301 302 303 304
      // #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
305
      let a: Int = 12
306 307 308 309 310 311 312
      let b = Number.from(a)
      console.log(b);
      // expected output: 12
      // #END
      expect(b).toEqual(12);
      // #endif
    })
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379

    test('numberEquals', () => {
      let a1 = 10.123
      let b1 = 10.12300
      let c1 = 10.12300002
      expect(a1 == b1).toEqual(true);
      expect(a1 === b1).toEqual(true);
      expect(a1 !== 10.123001).toEqual(true);
      expect(a1 != c1).toEqual(true);

      let a2 = 0
      let b2 = 0.0
      expect(a2 == b2).toEqual(true);
      expect(a2 === b2).toEqual(true);

      let a3 = -1000
      let b3 = -1000.0
      expect(a3 == b3).toEqual(true);
      expect(a3 === b3).toEqual(true);

      let a4 = 123456789123456
      let b4 = 123456789123456.0
      expect(a4 == b4).toEqual(true);
      expect(a4 === b4 * 1.0).toEqual(true);


      let a5 = -123456789123456
      let b5 = -123456789123456.01
      expect(a5 != b5).toEqual(true);
      expect(a5 !== b5).toEqual(true);


      let a6 = 246.0 / 123 * 1.0
      let b6 = 2
      let c6 = 2.0
      expect(a6 == b6).toEqual(true);
      expect(b6 === c6).toEqual(true);
      expect(b6 !== c6 * 1.001).toEqual(true);
      expect(b6 != c6 * 1.001).toEqual(true);

      let a7 = -10 * 123.2
      let b7 = - 1232
      expect(a7 == b7).toEqual(true);
      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);


    })


  })
}