YUILibrary - Open source JavaScript and CSS for building richly interactive software.
Fork YUI on GitHub

YUI 2.x

Ticket #2528651 (checkedin defect)

Reporter


matt marston
Opened: 11/23/09
Last modified: 11/24/09
Status: checkedin
Type: defect
Resolution: fixed

Owner


Luke Smith
Target Release: 2.NEXT
Priority: P3 (normal)
Summary: Function YAHOO.util.Number.format does not return empty string on null input
Description:

The documentation states that YAHOO.util.Number.format should return an empty string ("") for null, undefined, NaN, or empty string input. In 2.7.0 this worked as documented. In 2.8.0 null and empty
string return 0.

Type: defect Observed in Version: 2.8.0
Component: DataSource Severity: S3 (normal)
Assigned To: Luke Smith Target Release: 2.NEXT
Location: Library Code Priority: P3 (normal)
Tags: Relates To: #2527771
Browsers: All
URL:
Test Information:

output = YAHOO.util.Number.format(null);
Assert.areSame("", output, "Incorrect output from null.");

output = YAHOO.util.Number.format(undefined);
Assert.areSame("", output, "Incorrect output from undefined.");

output = YAHOO.util.Number.format(NaN);
Assert.areSame("", output, "Incorrect output from NaN.");

output = YAHOO.util.Number.format("");
Assert.areSame("", output, "Incorrect output from empty string.");

Change History

matt marston

Posted: 11/24/09
The following check in the old function

if(!lang.isValue(nData) || (nData === "")) {
return "";
}
was replaced by a check that performs implicit conversion to a number, which converts null and "" to 0

if (!isFinite(+n)) {
return '';
}

George

YUI Developer

Posted: 11/24/09

assigning to Luke for review

matt marston

Posted: 11/24/09

Another change in Number.format behavior is that the negative now comes before the prefix. I didn't file this as a bug because the documentation doesn't state the expected behavior.


output = YAHOO.util.Number.format("-1231.176", {
prefix: "$",
decimalPlaces: 2,
thousandsSeparator: ",",
decimalSeparator: ".",
negativeFormat: "-#"
});

In 2.7.0 this returned $-1,231.18 but in 2.8.0 it now returns -$1,231.18

matt marston

Posted: 11/24/09

Another change in Number.format behavior is that a non-numeric string now returns an empty string rather than NaN. I view this as a positive change in behavior.


output = YAHOO.util.Number.format("abc");

In 2.7.0 this returned NaN but in 2.8.0 it now returns ""

Luke Smith

YUI Developer

Posted: 11/24/09
  • location changed to Library Code
  • milestone changed to 2.NEXT
  • status changed from assigned to accepted

When I wrote the original version of the function as a proof of concept, it always returned a number. When that poc replaced the 2.7 version, it was tweaked to behave similarly. I didn't match up the docs completely, it seems.

In light of its support to return empty string for invalids, I agree that null and empty string should (still) return empty string.

In previous versions, there was no ability to format negative numbers. You're correct I broke backward compatibility with the feature enhancement. Thanks for catching that.

Luke Smith

YUI Developer

Posted: 11/24/09
  • status changed from accepted to checkedin

I've corrected the empty string/null => 0 issue. It will now return the empty string for these values as well.

To address the back compat issue related to the negativeFormat, I've expanded that feature with an additional config 'format' that acts as a string template for the output. negativeFormat also supports this. This also obviates prefix and suffix, which have been marked as deprecated. Example:


var conf = {
format: '${number}',
negativeFormat: '$-{number}',
thousandsSeparator: ',',
decimalPlaces: 2
};
YAHOO.util.Number.format(1234.5678, conf); // "$1,234.56"
YAHOO.util.Number.format(-9876.5432, conf); // "$-9,876.54"

By default, negativeFormat is the same as format, prefixed by - (per the current behavior). So


delete conf.negativeFormat;
YAHOO.util.Number.format(-9876.5432, conf); // "-$9,876.54"

I added all the tests you provided as well as a few extras for the format/negativeFormat feature enhancement. You can find the new version on GitHub if you'd like.

Luke Smith

YUI Developer

Posted: 11/24/09
  • resolution changed to fixed

Added format property support to config object for templating rather than fixed prefix and suffix properties. Expanded negativeFormat to also use this template system so $-123.45 is allowed again (back compat regression). null and empty string return empty string again (regression). [fixes #2528651]. Added tests for bug fix verification and for format/negativeFormat coverage. Other misc formatting fixes.
View Commit: