rounding carry

This commit is contained in:
Max Walker 2015-07-13 20:05:16 -04:00 committed by SheetJS
parent 695452bffb
commit 4fd36de071
3 changed files with 42 additions and 19 deletions

15
ssf.js

@ -310,7 +310,18 @@ function hashq(str/*:string*/)/*:string*/ {
return o;
}
function rnd(val/*:number*/, d/*:number*/)/*:string*/ { var dd = Math.pow(10,d); return ""+(Math.round(val * dd)/dd); }
function dec(val/*:number*/, d/*:number*/)/*:number*/ { return Math.round((val-Math.floor(val))*Math.pow(10,d)); }
function dec(val/*:number*/, d/*:number*/)/*:number*/ {
if (d < ('' + Math.round((val-Math.floor(val))*Math.pow(10,d))).length) {
return 0;
}
return Math.round((val-Math.floor(val))*Math.pow(10,d));
}
function carry(val/*:number*/, d/*:number*/)/*:number*/ {
if (d < ('' + Math.round((val-Math.floor(val))*Math.pow(10,d))).length) {
return 1;
}
return 0;
}
function flr(val/*:number*/)/*:string*/ { if(val < 2147483647 && val > -2147483648) return ""+(val >= 0 ? (val|0) : (val-1|0)); return ""+Math.floor(val); }
function write_num_flt(type/*:string*/, fmt/*:string*/, val/*:number*/)/*:string*/ {
if(type.charCodeAt(0) === 40 && !fmt.match(closeparen)) {
@ -342,7 +353,7 @@ function write_num_flt(type/*:string*/, fmt/*:string*/, val/*:number*/)/*:string
}
if((r = fmt.match(/^#,##0(\.?)$/))) return sign + commaify(pad0r(aval,0));
if((r = fmt.match(/^#,##0\.([#0]*0)$/))) {
return val < 0 ? "-" + write_num_flt(type, fmt, -val) : commaify(""+(Math.floor(val))) + "." + pad0(dec(val, r[1].length),r[1].length);
return val < 0 ? "-" + write_num_flt(type, fmt, -val) : commaify(""+(Math.floor(val) + carry(val, r[1].length))) + "." + pad0(dec(val, r[1].length),r[1].length);
}
if((r = fmt.match(/^#,#*,#0/))) return write_num_flt(type,fmt.replace(/^#,#*,/,""),val);
if((r = fmt.match(/^([0#]+)(\\?-([0#]+))+$/))) {

15
ssf.md

@ -537,7 +537,18 @@ V8 has an annoying habit of deoptimizing round and floor
```
function rnd(val/*:number*/, d/*:number*/)/*:string*/ { var dd = Math.pow(10,d); return ""+(Math.round(val * dd)/dd); }
function dec(val/*:number*/, d/*:number*/)/*:number*/ { return Math.round((val-Math.floor(val))*Math.pow(10,d)); }
function dec(val/*:number*/, d/*:number*/)/*:number*/ {
if (d < ('' + Math.round((val-Math.floor(val))*Math.pow(10,d))).length) {
return 0;
}
return Math.round((val-Math.floor(val))*Math.pow(10,d));
}
function carry(val/*:number*/, d/*:number*/)/*:number*/ {
if (d < ('' + Math.round((val-Math.floor(val))*Math.pow(10,d))).length) {
return 1;
}
return 0;
}
function flr(val/*:number*/)/*:string*/ { if(val < 2147483647 && val > -2147483648) return ""+(val >= 0 ? (val|0) : (val-1|0)); return ""+Math.floor(val); }
```
@ -614,7 +625,7 @@ The next few simplifications ignore leading optional sigils (`#`):
}
if((r = fmt.match(/^#,##0(\.?)$/))) return sign + commaify(pad0r(aval,0));
if((r = fmt.match(/^#,##0\.([#0]*0)$/))) {
return val < 0 ? "-" + write_num_flt(type, fmt, -val) : commaify(""+(Math.floor(val))) + "." + pad0(dec(val, r[1].length),r[1].length);
return val < 0 ? "-" + write_num_flt(type, fmt, -val) : commaify(""+(Math.floor(val) + carry(val, r[1].length))) + "." + pad0(dec(val, r[1].length),r[1].length);
}
if((r = fmt.match(/^#,#*,#0/))) return write_num_flt(type,fmt.replace(/^#,#*,/,""),val);
```

@ -1,15 +1,16 @@
value #.0000,,, #.0000,, #.0000,
1.2345 .0000 .0000 .0012
12.345 .0000 .0000 .0123
123.456 .0000 .0001 .1235
1234 .0000 .0012 1.2340
12345 .0000 .0123 12.3450
123456 .0001 .1235 123.4560
1234567 .0012 1.2346 1234.5670
12345678 .0123 12.3457 12345.6780
123456789 .1235 123.4568 123456.7890
1234567890 1.2346 1234.5679 1234567.8900
12345678901 12.3457 12345.6789 12345678.9010
123456789012 123.4568 123456.7890 123456789.0120
4321 .0000 .0043 4.3210
4321234 .0043 4.3212 4321.2340
value #.0000,,, #.0000,, #.0000, #,##0.0
0.99 .0000 .0000 .0010 1.0
1.2345 .0000 .0000 .0012 1.2
12.345 .0000 .0000 .0123 12.3
123.456 .0000 .0001 .1235 123.5
1234 .0000 .0012 1.2340 1,234.0
12345 .0000 .0123 12.3450 12,345.0
123456 .0001 .1235 123.4560 123,456.0
1234567 .0012 1.2346 1234.5670 1,234,567.0
12345678 .0123 12.3457 12345.6780 12,345,678.0
123456789 .1235 123.4568 123456.7890 123,456,789.0
1234567890 1.2346 1234.5679 1234567.8900 1,234,567,890.0
12345678901 12.3457 12345.6789 12345678.9010 12,345,678,901.0
123456789012 123.4568 123456.7890 123456789.0120 123,456,789,012.0
4321 .0000 .0043 4.3210 4,321.0
4321234 .0043 4.3212 4321.2340 4,321,234.0

1 value #.0000,,, #.0000,, #.0000, #,##0.0
2 1.2345 0.99 .0000 .0000 .0012 .0010 1.0
3 12.345 1.2345 .0000 .0000 .0123 .0012 1.2
4 123.456 12.345 .0000 .0001 .0000 .1235 .0123 12.3
5 1234 123.456 .0000 .0012 .0001 1.2340 .1235 123.5
6 12345 1234 .0000 .0123 .0012 12.3450 1.2340 1,234.0
7 123456 12345 .0001 .0000 .1235 .0123 123.4560 12.3450 12,345.0
8 1234567 123456 .0012 .0001 1.2346 .1235 1234.5670 123.4560 123,456.0
9 12345678 1234567 .0123 .0012 12.3457 1.2346 12345.6780 1234.5670 1,234,567.0
10 123456789 12345678 .1235 .0123 123.4568 12.3457 123456.7890 12345.6780 12,345,678.0
11 1234567890 123456789 1.2346 .1235 1234.5679 123.4568 1234567.8900 123456.7890 123,456,789.0
12 12345678901 1234567890 12.3457 1.2346 12345.6789 1234.5679 12345678.9010 1234567.8900 1,234,567,890.0
13 123456789012 12345678901 123.4568 12.3457 123456.7890 12345.6789 123456789.0120 12345678.9010 12,345,678,901.0
14 4321 123456789012 .0000 123.4568 .0043 123456.7890 4.3210 123456789.0120 123,456,789,012.0
15 4321234 4321 .0043 .0000 4.3212 .0043 4321.2340 4.3210 4,321.0
16 4321234 .0043 4.3212 4321.2340 4,321,234.0