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-2824_numbers rounding #2809

Open
wants to merge 1 commit 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
2 changes: 1 addition & 1 deletion packages/ssf/bits/59_numhelp.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var pct1 = /%/g;
function write_num_pct(type/*:string*/, fmt/*:string*/, val/*:number*/)/*:string*/{
var sfmt = fmt.replace(pct1,""), mul = fmt.length - sfmt.length;
return write_num(type, sfmt, val * Math.pow(10,2*mul)) + fill("%",mul);
return write_num(type, sfmt, times_ten_to_power(val, 2*mul)) + fill("%",mul);
}
14 changes: 12 additions & 2 deletions packages/ssf/bits/60_number.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
function write_num_cm(type/*:string*/, fmt/*:string*/, val/*:number*/)/*:string*/{
var idx = fmt.length - 1;
while(fmt.charCodeAt(idx-1) === 44) --idx;
return write_num(type, fmt.substr(0,idx), val / Math.pow(10,3*(fmt.length-idx)));
var d = 3*((fmt.length-idx)*-1);
return write_num(type, fmt.substr(0,idx), times_ten_to_power(val, d));
}
function times_ten_to_power(val/*:number*/, power/*:number*/)/*:number*/ {
var isNegative = Math.sign(val) === -1;
var parts = val.toString().split('e');
if (isNegative && !parts[0].startsWith('-')) parts[0] = '-' + parts[0];
return +(parts[0] + 'e' + ((parts[1] ? +parts[1] : 0) + power));
}
function write_num_exp(fmt/*:string*/, val/*:number*/)/*:string*/{
var o/*:string*/;
Expand Down Expand Up @@ -51,7 +58,10 @@ 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 dd = Math.round(times_ten_to_power(val, d));
return times_ten_to_power(dd, (d*-1))+"";
}
function dec(val/*:number*/, d/*:number*/)/*:number*/ {
var _frac = val - Math.floor(val), dd = Math.pow(10,d);
if (d < ('' + Math.round(_frac * dd)).length) return 0;
Expand Down