Can JSON numbers be quoted?

In JSON, 6 is the number six. “6” is a string containing the digit 6. So the answer to the question “Can json numbers be quoted?” is basically “no,” because if you put them in quotes, they’re not numbers anymore. But, should the parsers accept both “attr” : 6 and “attr” : “6”? Yes, but … Read more

Javascript: How to retrieve the number of decimals of a *string* number?

function decimalPlaces(num) { var match = (”+num).match(/(?:\.(\d+))?(?:[eE]([+-]?\d+))?$/); if (!match) { return 0; } return Math.max( 0, // Number of digits right of decimal point. (match[1] ? match[1].length : 0) // Adjust for scientific notation. – (match[2] ? +match[2] : 0)); } The extra complexity is to handle scientific notation so decimalPlaces(‘.05’) 2 decimalPlaces(‘.5’) 1 decimalPlaces(‘1’) … Read more