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

fixes formatting and parsing of numbers #396

Open
wants to merge 4 commits into
base: cc
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
21 changes: 14 additions & 7 deletions src/Core/Scripts/Runtime/Format.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ function _commaFormatNumber(number, groups, decimal, comma) {
number = number.substr(0, decimalIndex);
}

var negative = number.startsWith('-');
var negative = ss.startsWith(number, '-');
if (negative) {
number = number.substr(1);
}

var groupIndex = 0;
var groupSize = groups[groupIndex];
if (number.length < groupSize) {
if (negative) {
number = '-' + number;
}
return decimalPart ? number + decimalPart : number;
}

Expand Down Expand Up @@ -72,7 +75,7 @@ _formatters['Number'] = function(number, format, culture) {
if (precision != -1) {
s = padLeft(s, precision, '0');
}
if (number < 0) {
if (number < 0 && s != 0) {
s = '-' + s;
}
break;
Expand All @@ -92,6 +95,10 @@ _formatters['Number'] = function(number, format, culture) {
else {
s = number.toExponential(precision);
}
if (nf.ds != '.') {
var index = s.indexOf('.');
s = s.substr(0, index) + nf.ds + s.substr(index + 1);
}
if (fs == 'E') {
s = s.toUpperCase();
}
Expand All @@ -101,7 +108,7 @@ _formatters['Number'] = function(number, format, culture) {
if (precision == -1) {
precision = nf.dd;
}
s = number.toFixed(precision).toString();
s = (+(Math.round(+(number + 'e' + precision)) + 'e' + -precision)).toFixed(precision);
if (precision && (nf.ds != '.')) {
var index = s.indexOf('.');
s = s.substr(0, index) + nf.ds + s.substr(index + 1);
Expand All @@ -121,10 +128,10 @@ _formatters['Number'] = function(number, format, culture) {
}
s = _commaFormatNumber(s, nf.curGW, nf.curDS, nf.curGS);
if (number < 0) {
s = String.format(culture, nf.curNP, s);
s = ss.format(culture, nf.curNP, s);
}
else {
s = String.format(culture, nf.curPP, s);
s = ss.format(culture, nf.curPP, s);
}
break;
case 'p': case 'P':
Expand All @@ -138,10 +145,10 @@ _formatters['Number'] = function(number, format, culture) {
}
s = _commaFormatNumber(s, nf.perGW, nf.perDS, nf.perGS);
if (number < 0) {
s = String.format(culture, nf.perNP, s);
s = ss.format(culture, nf.perNP, s);
}
else {
s = String.format(culture, nf.perPP, s);
s = ss.format(culture, nf.perPP, s);
}
break;
}
Expand Down
6 changes: 6 additions & 0 deletions src/Core/Scripts/Runtime/Misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ function parseNumber(s) {
if (!s || !s.length) {
return 0;
}

if (ss.culture.current.nf.gs.length)
Copy link
Owner

Choose a reason for hiding this comment

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

The parse functions aren't yet setup for globalization (unlike the format functions). The intent here was to only parse per the invariant/neutral culture.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I was thinking to introduce a parseNumber(culture, s) but as you said, would be inconsistent to parseDate where culture awarenes is much more challenging. Nevertheless when coming from .Net one expects parsing to be culture aware, which in case of Numbers is easy to achieve. So I thought I would implement a robust way in the framework, rather to writing my own convert logic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hum, I overcame a situation where forcibly parsing with currentCulture is not desireable:
jQuery.Select("#ID").GetCSS("font-size") => may return "22.5pt"
With my addition parseNumber("22.5pt") gets 225.
So I'll better keep my own version of culture aware parsing in my utils.

s = s.replace(new RegExp('\\' + ss.culture.current.nf.gs, 'g'), '');
if (ss.culture.current.nf.ds.length)
s = s.replace(new RegExp('\\' + ss.culture.current.nf.ds), '.');

if ((s.indexOf('.') >= 0) || (s.indexOf('e') >= 0) ||
endsWith(s, 'f') || endsWith(s, 'F')) {
return parseFloat(s);
Expand Down