Skip to content

Commit

Permalink
Added # flag support for o, x, and X types
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr0grog committed May 14, 2012
1 parent 01f6ae8 commit b8e573a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ES6StringFormat-tests.js
Expand Up @@ -70,8 +70,11 @@ assertEquals("{0:.4}".format(5), "5.0000", "A precision specifier results in at
assertEquals("{0:.4}".format(5.14326), "5.14326", "A precision specifier does not truncate a number that is more precise.");
assertEquals("{0:b}".format(10), "1010", "The 'b' type converts a number to binary.");
assertEquals("{0:o}".format(10), "12", "The 'o' type converts a number to octal.");
assertEquals("{0:#o}".format(10), "012", "The 'o' type converts a number to octal with a preceding 0 when the '#' flag is used.");
assertEquals("{0:x}".format(10), "a", "The 'x' type converts a number to lower-case hexadecimal.");
assertEquals("{0:#x}".format(10), "0xa", "The 'x' type converts a number to lower-case hexadecimal with a preceding 0x when the '#' flag is used.");
assertEquals("{0:X}".format(10), "A", "The 'X' type converts a number to upper-case hexadecimal.");
assertEquals("{0:#X}".format(10), "0XA", "The 'X' type converts a number to upper-case hexadecimal with a preceding 0X when the '#' flag is used.");

assertEquals("{0:e}".format(5), "5 e+00", "The 'e' type converts to scientific notation with at least two digits in the exponent.");
assertEquals("{0:E}".format(5), "5 E+00", "The 'E' type converts to upper-case scientific notation with at least two digits in the exponent.");
Expand Down
9 changes: 9 additions & 0 deletions ES6StringFormat.js
Expand Up @@ -179,9 +179,15 @@
// hex
case "x":
result = Math.round(value - 0.5).toString(16);
if (~flags.indexOf("#")) {
result = "0x" + result;
}
break;
case "X":
result = Math.round(value - 0.5).toString(16).toUpperCase();
if (~flags.indexOf("#")) {
result = "0X" + result;
}
break;
// binary
case "b":
Expand All @@ -190,6 +196,9 @@
// octal
case "o":
result = Math.round(value - 0.5).toString(8);
if (~flags.indexOf("#")) {
result = "0" + result;
}
break;
// scientific notation (exponential)
case "e":
Expand Down

0 comments on commit b8e573a

Please sign in to comment.