I used intl.formatNumber and it gave me what I needed:
formatNumber(1000); // "1,000"
I can then include the km. inside "defaultMessage" and let translators change that should they wish to.
Source: https://github.com/yahoo/react-intl/wiki/API#number-formatting-apis
Answer from Corkscreewe on Stack OverflowI used intl.formatNumber and it gave me what I needed:
formatNumber(1000); // "1,000"
I can then include the km. inside "defaultMessage" and let translators change that should they wish to.
Source: https://github.com/yahoo/react-intl/wiki/API#number-formatting-apis
This should format the number as you asked. I'm sure there are way better solutions but this should give you what you are looking for in the meantime.
formatNumber(val) {
const value = val.toString();
return `${value.substring(0, value.length - 3)}.${value.substring(value.length - 3, value.length)} km`;
}
render() {
return (
<div>
<p>{this.formatNumber(45000)}</p>
</div>
);
}
» npm install react-intl-number-format
Looks like you need the international version of JSC: Android: https://github.com/react-native-community/jsc-android-buildscripts#international-variant
For iOS you'd need to use Intl.js polyfill https://github.com/andyearnshaw/Intl.js/
Simply install the intl package for expo building on android. Then you can use the Intl.NumberFormat as described in the Mozilla Docs.
Javascript has a number formatter (part of the Internationalization API).
// Quick Solution With Intl.NumberFormat
const update = (val: any) => {
var formatter = new Intl.NumberFormat("en-US"); // Intl language tag,
updateValue(formatter.format(val.replace(/,/g, ""))); //Remove ',' to format number again
};
Code Snippet:
// Intl.NumberFormat With React State Update
var currentVal = 0;
...
const update = (event: any) => {
/**
https://stackoverflow.com/questions/35535688/stop-cursor-jumping-when-formatting-number-in-react
https://github.com/facebook/react/issues/955
*/
const caret = event.target.selectionStart
const element = event.target
window.requestAnimationFrame(() => {
element.selectionStart = caret
element.selectionEnd = caret
})
// -- Stop cursor jumping when formatting number in React
var val = event.target.value.replace(/(\..*)\./g, '$1') //Replace Multiple Dot(.)
var x = Number(val.replace(/,/g, ""));
if (currentVal != x) {
var formatter = new Intl.NumberFormat("en-US", { minimumFractionDigits:2});
currentVal = formatter.format(x);
updateValue(currentVal);
}else{
updateValue(val);
}
};
return (<input type="text" value={value} onChange={e => update(e)} />);
Note : Code Snippet gives you an idea to format numbers, You need to handle few more use-cases for production.
Also check the react-number-format, Which may suit for your application.
Reference :
- Intl.NumberFormat vs Number.prototype.toLocaleString
- How can I format numbers as dollars currency string in JavaScript?
- Intl.NumberFormat | MDN
The problem is in
const x = Number(val);
when you evaluate Number("32,423,343"), a string including commas Js will throw an error...
The correct way would be sending the number without commas.. Number("32432343")
To solve it you can add this line to remove the commas, before evaluating to a Number..
val = val.replace(/,/g, '');
https://codesandbox.io/s/r0vm8nxvjo
» npm install react-intl
» npm install react-number-format