Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for floating point rounding issue #34

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion bits/60_number.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,24 @@ 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 rnd(val/*:number*/, d/*:number*/)/*:string*/ {
var result
if(("" + val).indexOf("e") === -1) {
result = +(Math.round(val + "e+" + d) + "e-" + d);
} else {
var arr = ("" + val).split("e");
var sig = ""
if(+arr[1] + d > 0) {
sig = "+";
}
result = +(Math.round(+arr[0] + "e" + sig + (+arr[1] + d)) + "e-" + d);
}
if (isNaN(result)) {
var dd = Math.pow(10, d);
result = Math.round(val * dd) / dd;
}
return "" + result
}
function dec(val/*:number*/, d/*:number*/)/*:number*/ {
if (d < ('' + Math.round((val-Math.floor(val))*Math.pow(10,d))).length) {
return 0;
Expand Down
19 changes: 18 additions & 1 deletion ssf.flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,24 @@ 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 rnd(val/*:number*/, d/*:number*/)/*:string*/ {
var result
if(("" + val).indexOf("e") === -1) {
result = +(Math.round(val + "e+" + d) + "e-" + d);
} else {
var arr = ("" + val).split("e");
var sig = ""
if(+arr[1] + d > 0) {
sig = "+";
}
result = +(Math.round(+arr[0] + "e" + sig + (+arr[1] + d)) + "e-" + d);
}
if (isNaN(result)) {
var dd = Math.pow(10, d);
result = Math.round(val * dd) / dd;
}
return "" + result
}
function dec(val/*:number*/, d/*:number*/)/*:number*/ {
if (d < ('' + Math.round((val-Math.floor(val))*Math.pow(10,d))).length) {
return 0;
Expand Down
19 changes: 18 additions & 1 deletion ssf.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,24 @@ function hashq(str) {
}
return o;
}
function rnd(val, d) { var dd = Math.pow(10,d); return ""+(Math.round(val * dd)/dd); }
function rnd(val, d) {
var result
if(("" + val).indexOf("e") === -1) {
result = +(Math.round(val + "e+" + d) + "e-" + d);
} else {
var arr = ("" + val).split("e");
var sig = ""
if(+arr[1] + d > 0) {
sig = "+";
}
result = +(Math.round(+arr[0] + "e" + sig + (+arr[1] + d)) + "e-" + d);
}
if (isNaN(result)) {
var dd = Math.pow(10, d);
result = Math.round(val * dd) / dd;
}
return "" + result
}
function dec(val, d) {
if (d < ('' + Math.round((val-Math.floor(val))*Math.pow(10,d))).length) {
return 0;
Expand Down
2 changes: 2 additions & 0 deletions test/comma.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ value #.0000,,, #.0000,, #.0000, #,##0.0 ###,##0 ###,### #,###.00
123456789012 123.4568 123456.7890 123456789.0120 123,456,789,012.0 123,456,789,012 123,456,789,012 123,456,789,012.00
4321 .0000 .0043 4.3210 4,321.0 4,321 4,321 4,321.00
4321234 .0043 4.3212 4321.2340 4,321,234.0 4,321,234 4,321,234 4,321,234.00
1234652.68499999999995 .0012 1.2347 1234.6527 1,234,652.7 1,234,653 1,234,653 1,234,652.69
-4321234 -0.0043 -4.3212 -4321.2340 -4,321,234.0 -4,321,234 -4,321,234 -4,321,234.00
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first result should be -.0043 (no leading '0')