-2

I am stuck in a solution in which I am trying to convert the string to integer, but it truncates the zeroes after comma.Any help on this is much appreciated. For example,

parseInt("3,50,000") // 3  ==> what i actually need is 3,50,00 of integer type

enter image description here

Nancy
  • 911
  • 7
  • 26
  • 54
  • 1
    Remove the commas first, as `parseInt` will only parse up to the first invalid character. Also, *"what i actually need is 3,50,00 of integer type"* - `3,50,00` *isn't* an integer, it's a string. – Tyler Roper Feb 25 '20 at 14:27
  • That number isn't a valid integer. parseInt stops parsing at the first non-numeric character. [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt): "If parseInt encounters a character that is not a numeral in the specified radix, it ignores it and all succeeding characters and returns the integer value parsed up to that point. parseInt truncates numbers to integer values." –  Feb 25 '20 at 14:29
  • 1
    @TylerRoper: it is. In any locale that uses "," as a grouping separator. The grouping size indicates that this is for an Indian locale and they do indeed use "," for grouping and "." as the decimal separator. – Joachim Sauer Feb 25 '20 at 14:29
  • 1
    Does this answer your question? [How can I parse a string with a comma thousand separator to a number?](https://stackoverflow.com/questions/11665884/how-can-i-parse-a-string-with-a-comma-thousand-separator-to-a-number) – Joachim Sauer Feb 25 '20 at 14:30
  • @JoachimSauer Neither grouping separators nor decimals are part of valid integers though, no? – Tyler Roper Feb 25 '20 at 14:31
  • Two questions; why is `,50,` a group, and why would `3,50,00` be the expected outcome? – Ja͢ck Feb 25 '20 at 14:31
  • @TylerRoper: if by "integer" you mean the narrow definition used in programming languages then yes. But when interpreting user input that definition is often pretty useless. When users are asked to input whole numbers they might conceivably enter grouping separators and we should be able to accept those (in the same way a well-writen UI will produce grouping separators when presenting numbers to the user). – Joachim Sauer Feb 25 '20 at 14:35
  • I'm not sure what you're trying to do. Even as a human I would have a hard time knowing the expected result of a number with 2 decimal parts (AFAIK this doesn't exist) – Laurent S. Feb 25 '20 at 15:11
  • @LaurentS. "," here is not used as a decimal separator, but as a grouping separator. – Joachim Sauer Feb 25 '20 at 15:48
  • But then why group 2 digits together instead of 3? – Laurent S. Feb 26 '20 at 06:39
  • @LaurentS. Because the number given is 3.5 [_lakh_](https://en.wikipedia.org/wiki/Lakh), and that’s how those are written. – Janus Bahs Jacquet Oct 07 '20 at 12:43

2 Answers2

2

This:

console.log(parseInt("3,50,000".replace(/,/ig, ''), 10));

Displays:

350000

You must remove commas used for thousands (, millions...) markers (here I replace with empty strings) before parsing an int from a string.

Also, always include the radix, here 10 meaning parse as a base 10 integer.

I used a regular expression to perform the replacement because it is quite efficient, having to process the string only once.

Will
  • 6,601
  • 3
  • 31
  • 42
2

console.log(parseInt("3,50,000".split(',').join('')))
kooskoos
  • 4,622
  • 1
  • 12
  • 29