A would like to convert Full-width Numbers (e.g. 123) to half-width Numbers (e.g. 123). I found code do this in PHP but not in JS. Could anyone helps? Thanks.
function fullWidthNumConvert(fullWidthNum){
// Magic here....
....
return halfWidthNum;
}
Do a string .replace(), using a regular expression to match the characters in question. The callback in .replace()'s second argument can get the character code of the matched character and subtract from that to get the character code of the standard digit, then convert that back to a string.
function fullWidthNumConvert(fullWidthNum){
return fullWidthNum.replace(/[\uFF10-\uFF19]/g, function(m) {
return String.fromCharCode(m.charCodeAt(0) - 0xfee0);
});
}
console.log(fullWidthNumConvert("0123456789"));
console.log(fullWidthNumConvert("Or in the middle of other text: 123. Thank you."));
Using String Normalize MDN with "NFKC" as the Unicode Normalization Form
const str = "150721";
console.log(str.normalize('NFKC')); // 150721
https://www.unicode.org/charts/normalization/chart_Number-Decimal.html