When converting a decimal number to a base above 10 using .toString(base)
, it seems that I always get a lower case string. Can I rely on this? An upper case string would be correct, although would need converting for my application.
Extra credit for referencing the part of the spec that defines this (I looked and couldn't find it) and for any counter-examples (browsers that return uppercase).
Example:
(12648430).toString(16) // returns: "c0ffee". Not "C0FFEE"
If the String object returned by the ToString (IFormatProvider) method is to be written to an XML file, its String.ToLowerInvariant method should be called first to convert it to lowercase. Thanks for contributing an answer to Stack Overflow!
The toString () method does not change the original string. The toString () method can be used to convert a string object into a string. Every JavaScript object has a toString () method.
The toLowerCase () method converts a string to lowercase letters. The toLowerCase () method does not change the original string. The string converted to lowercase.
[ToString. ()] returns the constants "True" or "False". Note that XML is case-sensitive, and that the XML specification recognizes "true" and "false" as the valid set of Boolean values.
Yes, it's always lower case. That's been defined in the specification since the 5th edition in 2009. Here's what it says in the 5.1 spec; 5.0 isn't directly linkable, but it said much the same thing:
If ToInteger(radix) is not an integer between 2 and 36 inclusive throw a RangeError exception. If ToInteger(radix) is an integer from 2 to 36, but not 10, the result is a String representation of this Number value using the specified radix. Letters a-z are used for digits with values 10 through 35. The precise algorithm is implementation-dependent if the radix is not 10, however the algorithm should be a generalisation of that specified in 9.8.1.
(my emphasis)
The current spec continues to specify using a-z
(in lower case).
Previously, the 3rd edition spec (there was no 4th edition) from 1999 did not say that, it just said:
If radix is an integer from 2 to 36, but not 10, the result is a string, the choice of which is implementation-dependent.
...but now that browsers that were created when the 3rd edition spec was the current spec are well and truly gone, you can rely on it being in lower case. And indeed, you probably could even when this answer was first posted in 2013, since they didn't usually add that kind of thing to the specification if there were significant known implementations that did something different.
Just for fun, here's a quick check:
const str = (12648430).toString(16);
console.log(`${str} === c0ffee? ${str === "c0ffee"}`);
(12648430).toString(16) will always return: "c0ffee". Not "C0FFEE", after checking it with somes browsers, I found a confirmation:
The Number object overrides the toString() method of the Object object; it does not
inherit Object.prototype.toString(). For Number objects, the toString() method returns a string representation of the object in the specified radix.The toString() method parses its first argument, and attempts to return a string
representation in the specified radix (base). For radixes above 10, the letters of the alphabet indicate numerals greater than 9. For example, for hexadecimal numbers (base 16), a through f are used.
See reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toString .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With