📄 number.js
字号:
//,"12,34,56, that ",//[2] //"12,34,56 that", //[3] ]); runBatchParse(options,mixedFailData,false); /**************************************** strict parse ****************************************** * TODO:May need to test strict parsing in the future? * e.g. A strict parsing like (with pattern "#,##0.#") * 1.Leading zeros * '00', '0123' fail the parse, but '0' and '0.001' pass * 2.Leading or doubled grouping separators * ',123' and '1,,234" fail * 3.Groups of incorrect length when grouping is used * '1,23' and '1234,567' fail, but '1234' passes * 4.Grouping separators used in numbers followed by exponents * '1,234E5' fails, but '1234E5' and '1,234E' pass */ //options={locale:"en",strict:true}; //runBatchParse(options,passData,false/*strict parse*/); //runBatchParse(options,failData,false/*strict parse*/); //options = {pattern:"#,##,##0.#",locale:"en-us",strict:true}; //runBatchParse(options,mixedPassData,false/*strict parse*/); //runBatchParse(options,mixedFailData,false/*strict parse*/); //print("test_number_parse_icu4j3_6() end..............\n"); } }, { name: "parse_whitespace", runTest: function(t){/** * TODO:!!Failed case * With pattern "a b#0c ",both "a b3456c " and and "a b1234c " should be parsed to 3456,but got NaN instead. * * Refer to ICU4J's NumberFormatTest.TestWhiteSpaceParsing */ /* print("test_number_parse_WhiteSpace() start.............."); var pattern = "a b#0c "; var expectResult = 3456; result = dojo.number.parse("a b3456c ",{pattern:pattern,locale:"en-us"}); t.is(expectResult,result); result = dojo.number.parse("a b3456c ",{pattern:pattern,locale:"en-us"}); t.is(expectResult,result); print("test_number_parse_WhiteSpace() end..............\n"); */ } },/************************************************************************************************* * Regression test cases * These test cases are referred to ICU4J's NumberFormatRegressionTest and NumberFormatRegression. * The regression cases in ICU4J are used as unit test cases for bug fixing, * They are inluced here so that dojo.number may avoid those similar bugs. *************************************************************************************************/ { name: "number_regression_1", runTest: function(t){/** * Refer to ICU4J's NumberFormatRegressionTest.Test4161100() */ tests.number.checkFormatParseCycle(t, {pattern:"#0.#"},-0.09,"-0.1",false); } }, { name: "number_regression_2", runTest: function(t){/** * !!Failed case,rounding hasn't been implemented yet. * Refer to ICU4J's NumberFormatRegressionTest.Test4408066() */ /* var data = ([-3.75, -2.5, -1.5, -1.25, 0, 1.0, 1.25, 1.5, 2.5, 3.75, 10.0, 255.5]); var expected = (["-4", "-2", "-2", "-1", "0", "1", "1", "2", "2", "4", "10", "256"]); var options = {locale:"zh-cn",round:true}; for(var i =0; i < data.length; i++){ tests.number.checkFormatParseCycle(t, options,data[i],expected[i],false); } data = ([ "-3.75", "-2.5", "-1.5", "-1.25", "0", "1.0", "1.25", "1.5", "2.5", "3.75", "10.0", "255.5"]); expected =([ -3, -2, -1, -1, 0, 1, 1, 1, 2, 3, 10, 255]); for(var i =0; i < data.length; i++){ tests.number.checkParse(t, options,data[i],expected[i]); } */ } }, { name: "number_regression_3", runTest: function(t){/** * Refer to ICU4J's NumberRegression.Test4087535() and Test4243108() */ tests.number.checkFormatParseCycle(t, {places:0},0,"0",false); //TODO:in icu4j,0.1 should be formatted to ".1" when minimumIntegerDigits=0 tests.number.checkFormatParseCycle(t, {places:0},0.1,"0",false); tests.number.checkParse(t, {pattern:"#0.#####"},123.55456,123.55456);//!! fails because default pattern only has 3 decimal places// tests.number.checkParse(t, null,123.55456,123.55456); //See whether it fails first format 0.0 ,parse "99.99",and then reformat 0.0 tests.number.checkFormatParseCycle(t, {pattern:"#.#"},0.0,"0",false); tests.number.checkParse(t, null,"99.99",99.99); tests.number.checkFormatParseCycle(t, {pattern:"#.#"},0.0,"0",false); } }, { name: "number_regression_4", runTest: function(t){/** * TODO: * In ICU -0.0 and -0.0001 should be formatted to "-0" with FieldPosition(0) * dojo.i18n.number format -0.0 to "-0"; -0.0001 to "-0.000100" * * Refer to ICU4J's NumberRegression.Test4088503() and Test4106658() */ tests.number.checkFormatParseCycle(t, {places:0},123,"123",false); //TODO: differernt from ICU where -0.0 is formatted to "-0" tests.number.checkFormatParseCycle(t, {locale:"en-us"},-0.0,"0",false); //TODO: differernt from ICU where -0.0001 is formatted to "-0" tests.number.checkFormatParseCycle(t, {locale:"en-us",places:6},-0.0001,"-0.000100",false); } }, { name: "number_regression_5", runTest: function(t){/** * !!Failed case,rounding has not implemented yet. * 0.00159999 should be formatted as 0.0016 but got 0.0015 instead. * Refer to ICU4J's NumberRegression.Test4071492() */ //tests.number.checkFormatParseCycle(t, {places:4,round:true},0.00159999,"0.0016",false); } }, { name: "number_regression_6", runTest: function(t){/** * Refer to ICU4J's NumberRegression.Test4086575() */ var pattern = "###.00;(###.00)"; var locale = "fr"; var options = {pattern:pattern,locale:locale}; //no group separator tests.number.checkFormatParseCycle(t, options,1234,"1234,00",false); tests.number.checkFormatParseCycle(t, options,-1234,"(1234,00)",false); //space as group separator pattern = "#,###.00;(#,###.00)"; options = {pattern:pattern,locale:locale}; tests.number.checkFormatParseCycle(t, options,1234,"1\u00a0234,00",false);// Expect 1 234,00 tests.number.checkFormatParseCycle(t, options,-1234,"(1\u00a0234,00)",false); // Expect (1 234,00) } }, { name: "number_regression_7", runTest: function(t){/** * !!Failed case - expontent has not implemented yet * shuold format 1.000000000000001E7 to 10000000.00000001, but got 10,000,000.000 instead * Refer to ICU4J's NumberRegression.Test4090489() - loses precision */ //tests.number.checkFormatParseCycle(t, null,1.000000000000001E7,"10000000.00000001",false); } }, { name: "number_regression_8", runTest: function(t){/** * !!Failed case * 1.with pattern "#,#00.00 p''ieces;-#,#00.00 p''ieces" * 3456.78 should be formated to "3,456.78 p'ieces", * but got "3,456.78 p''ieces","''" should be replaced with "'" * 2.with illegal pattern "000.0#0" * no error for the illegal pattern, and 3456.78 is formatted to 456.780 * 3.with illegal pattern "0#0.000" * no error for the illegal pattern, and 3456.78 is formatted to 3456.780 * * Refer to ICU4J's NumberRegression.Test4092480(),Test4074454() */ var patterns = (["#0000","#000","#00","#0","#"]); var expect = (["0042","042","42","42","42"]); for(var i =0; i < patterns.length; i ++){ tests.number.checkFormatParseCycle(t, {pattern:patterns[i]},42,expect[i],false); tests.number.checkFormatParseCycle(t, {pattern:patterns[i]},-42,"-"+expect[i],false); } tests.number.checkFormatParseCycle(t, {pattern:"#,#00.00;-#.#"},3456.78,"3,456.78",false); //!!Failed case //tests.number.checkFormatParseCycle(t, {pattern:"#,#00.00 p''ieces;-#,#00.00 p''ieces"},3456.78,"3,456.78 p'ieces",false); //tests.number.checkFormatParseCycle(t, {pattern:"000.0#0"},3456.78,null,false); //tests.number.checkFormatParseCycle(t, {pattern:"0#0.000"},3456.78,null,false); } }, { name: "number_regression_9", runTest: function(t){/** * TODO * Refer to ICU4J's NumberRegression.Test4052223() */ //TODO:only got NaN,need an illegal pattern exception? tests.number.checkParse(t, {pattern:"#,#00.00"},"abc3"); //TODO: got NaN instead of 1.222, is it ok? //tests.number.checkParse(t, {pattern:"#,##0.###",locale:"en-us"},"1.222,111",1.222); //tests.number.checkParse(t, {pattern:"#,##0.###",locale:"en-us"},"1.222x111",1.222); //got NaN for illeal input,ok tests.number.checkParse(t, null,"hello: ,.#$@^&**10x"); } }, { name: "number_regression_10", runTest: function(t){/** * Refer to ICU4J's NumberRegression.Test4125885() */ tests.number.checkFormatParseCycle(t, {pattern:"000.00"},12.34,"012.34",false); tests.number.checkFormatParseCycle(t, {pattern:"+000.00%;-000.00%"},0.1234,"+012.34%",false); tests.number.checkFormatParseCycle(t, {pattern:"##,###,###.00"},9.02,"9.02",false); var patterns =(["#.00", "0.00", "00.00", "#0.0#", "#0.00"]); var expect = (["1.20", "1.20", "01.20", "1.2", "1.20" ]); for(var i =0 ; i < patterns.length; i ++){ tests.number.checkFormatParseCycle(t, {pattern:patterns[i]},1.2,expect[i],false); } } }, { name: "number_regression_11", runTest: function(t){/** * TODO:!!Failed case * Make sure that all special characters, when quoted in a suffix or prefix, lose their special meaning. * The detail error info : * for input 123 * pattern:'0'#0'0'; expect:"01230"; but got "'3'#0'0'" instead * pattern:','#0','; expect:",123,"; but got "','123','" instead * pattern:'.'#0'.'; expect:".123."; but got "'.'123'.'" instead * pattern:'‰'#0'‰'; expect:"‰123‰"; but got "'‰'123000'‰'" instead * pattern:'%'#0'%'; expect:"%123%"; but got "'%'12300'%'" instead * pattern:'#'#0'#'; expect:"#123#"; but got "'123'#0'#'" instead * pattern:';'#0';'; expect:";123;"; but got "[dojo-test] FATAL exception raised: * unable to find a number expression in pattern: '" * pattern:'E'#0'E'; expect:"E123E"; not implemeted yet * pattern:'*'#0'*'; expect:"*123*"; but got "'*'123'*'" instead * pattern:'+'#0'+'; expect:"+123+"; but got "'+'123'+'" instead * pattern:'-'#0'-'; expect:"-123-"; but got "'-'123'-'" instead * * TODO: is it ok to remain "'" in the formatted result as above?? * * Refer to ICU4J's NumberRegression.Test4212072() *//* var specials = ([ '0', ',', '.', '\u2030', '%', '#',';', 'E', '*', '+', '-']); var pattern; var expect; for(var i=0; i < specials.length; i ++){ pattern = "'" + specials[i] + "'#0'" + specials[i] + "'"; expect = "" + specials[i] + "123" + specials[i]; tests.number.checkFormatParseCycle(t, {pattern:pattern,locale:"en-us"},123,expect,false); }*/ } }, { name: "number_regression_12", runTest: function(t){/** * TODO: add more rounding test cases, refer to ICU4J's NumberRegression.Test4071005(),Test4071014() etc.. *//** * TODO:Decimal format doesnt round a double properly when the number is less than 1 * * Refer to ICU4J's NumberRegression.test4241880() *//* var input = ([ .019, .009, .015, .016, .014, .004, .005, .006, .007, .008, .5, 1.5, .05, .15, .005, .015, .0005, .0015]); var patterns = (["##0%", "##0%", "##0%", "##0%", "##0%", "##0%", "##0%", "##0%", "##0%", "##0%", "#,##0", "#,##0", "#,##0.0", "#,##0.0", "#,##0.00", "#,##0.00", "#,##0.000", "#,##0.000"]); var expect =([ "2%", "1%", "2%", "2%", "1%", "0%", "0%", "1%", "1%", "1%", "0", "2", "0.0", "0.2", "0.00", "0.02", "0.000", "0.002",]); for(var i = 0; i <input.length; i ++){ tests.number.checkFormatParseCycle(t, {pattern:patterns[i],round:true},input[i],expect[i],false); }*/ } } ]);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -