Format_enforceDecimalPlaces: enforcedecimalplaces_revision295.patch
| File enforcedecimalplaces_revision295.patch, 5.2 kB (added by mae<sseses@gmail.com>, 3 years ago) |
|---|
-
tests/test_MochiKit-Format.html
old new 13 13 try { 14 14 15 15 // Counting the number of tests is really lame 16 plan({'tests': 20});16 plan({'tests': 42}); 17 17 18 18 is( twoDigitFloat(-0.1234), "-0.12", "twoDigitFloat -0.1234 correct"); 19 19 is( twoDigitFloat(-0.1), "-0.1", "twoDigitFloat -0.1 correct"); … … 23 23 is( twoDigitFloat(1.0), "1", "twoDigitFloat 1.0 correct"); 24 24 is( twoDigitFloat(1.2), "1.2", "twoDigitFloat 1.2 correct"); 25 25 is( twoDigitFloat(1.234), "1.23", "twoDigitFloat 1.234 correct"); 26 27 // Make sure this new function holds up to all the tests twoDigitFloat does. 28 // Changed the output result to always have the specified places; Isn't this the desired 29 // behavior when formatting numbers like this? This is the printf behavior. 30 is( enforceDecimalPlaces(-0.1234, 2), "-0.12", "enforceDecimalPlaces -0.1234 correct"); 31 is( enforceDecimalPlaces(-0.1, 2), "-0.10", "enforceDecimalPlaces -0.1 correct"); 32 //'0' has no affinity to negative or positive in math, therefore it shouldn't here. 33 //This is the desired behavior with financial stuff also, I believe. 34 is( enforceDecimalPlaces(-0, 2), "0.00", "enforceDecimalPlaces -0 correct"); 35 is( enforceDecimalPlaces(0, 2), "0.00", "enforceDecimalPlaces 0 correct"); 36 is( enforceDecimalPlaces(1, 2), "1.00", "enforceDecimalPlaces 1 correct"); 37 is( enforceDecimalPlaces(1.0, 2), "1.00", "enforceDecimalPlaces 1.0 correct"); 38 is( enforceDecimalPlaces(1.2, 2), "1.20", "enforceDecimalPlaces 1.2 correct"); 39 is( enforceDecimalPlaces(1.234, 2), "1.23", "enforceDecimalPlaces 1.234 correct"); 40 //Should round up when appropriate. 41 is( enforceDecimalPlaces(1.236, 2), "1.24", "enforceDecimalPlaces 1.236 correct"); 42 43 //Make sure behavior with places = 0 works right 44 is( enforceDecimalPlaces(-0.1234, 0), "0", "enforceDecimalPlaces -0.1234 correct"); 45 is( enforceDecimalPlaces(-0.1, 0), "0", "enforceDecimalPlaces -0.1 correct"); 46 is( enforceDecimalPlaces(-0, 0), "0", "enforceDecimalPlaces -0 correct"); 47 is( enforceDecimalPlaces(0, 0), "0", "enforceDecimalPlaces 0 correct"); 48 is( enforceDecimalPlaces(1, 0), "1", "enforceDecimalPlaces 1 correct"); 49 is( enforceDecimalPlaces(1.0, 0), "1", "enforceDecimalPlaces 1.0 correct"); 50 is( enforceDecimalPlaces(1.2, 0), "1", "enforceDecimalPlaces 1.2 correct"); 51 is( enforceDecimalPlaces(1.234, 0), "1", "enforceDecimalPlaces 1.234 correct"); 52 //Should round up here 53 is( enforceDecimalPlaces(1.634, 0), "2", "enforceDecimalPlaces 1.234 correct"); 54 55 //Make sure it doesn't bomb out when you throw it the negative places grenade 56 is( enforceDecimalPlaces(1, -5324), "1", "enforceDecimalPlaces 1 correct"); 57 is( enforceDecimalPlaces(1.0, -324), "1", "enforceDecimalPlaces 1.0 correct"); 58 is( enforceDecimalPlaces(1.2, -999), "1", "enforceDecimalPlaces 1.2 correct"); 59 is( enforceDecimalPlaces(1.234, -1), "1", "enforceDecimalPlaces 1.234 correct"); 60 61 26 62 is( percentFormat(123), "12300%", "percentFormat 123 correct"); 27 63 is( percentFormat(1.23), "123%", "percentFormat 123 correct"); 28 64 is( twoDigitAverage(1, 0), "0", "twoDigitAverage dbz correct"); -
MochiKit/Format.js
old new 75 75 } 76 76 }; 77 77 78 MochiKit.Format.enforceDecimalPlaces = function (number, places) { 79 /*** 80 Expands or contracts number to specified places and rounds if 81 needed; returns a string. Equivalent to: sprintf("%.xf", someNumber) 82 where x is the number of decimal places you want. 83 84 (c) 2005 Matthew Elder <sseses@gmail.com> 85 ***/ 86 /* can't have negative values for places */ 87 if (places < 0) { places = 0; } 88 89 /* Pick apart pieces with a regexp */ 90 var numerical_components = /^(-?\d+)\.?(\d*)$/.exec(number); 91 var original = numerical_components[0]; 92 var integral = numerical_components[1]; 93 var fractional = numerical_components[2]; 94 95 var rval; 96 if (places <= 0) { 97 rval = String(Math.round(original)); 98 } else { 99 /* Setup the right side of decimal .XXXXX (integral) */ 100 if (places < fractional.length) { 101 var division_factor = fractional.length - places; 102 if (division_factor < 0) { division_factor = 0; } 103 fractional = Math.round(fractional / Math.pow(10, division_factor)); 104 } else if (places > fractional.length) { 105 var zeros_needed = places - fractional.length; 106 for (; zeros_needed; --zeros_needed) { 107 fractional += '0'; 108 } 109 } 110 rval = integral + '.' + fractional; 111 } 112 return rval; 113 }; 114 78 115 MochiKit.Format.lstrip = function (str, /* optional */chars) { 79 116 if (!chars) { 80 117 return str.replace(/^\s+/, ""); … … 108 145 MochiKit.Format.EXPORT = [ 109 146 "twoDigitAverage", 110 147 "twoDigitFloat", 148 "enforceDecimalPlaces", 111 149 "percentFormat", 112 150 "lstrip", 113 151 "rstrip",
