A Simple Example of javascript Converting Chinese Digital Format to European Digital Format

  • 2021-07-07 06:05:58
  • OfStack

There is a requirement in the project. It is necessary to change the Chinese way of displaying Arabic numerals to European way, that is, every three digits are displayed, separated by commas. For example, 12345678 is changed to 12,345 and 678. The following is the specific implementation of javascript code:


var iValue = 20002365879; // Number to convert 
var sValue = iValue+'';
var aValue = new Array();
var iNum = sValue.length%3;
var aResult; // Conversion result 
var index = 0;
if(sValue.length<=3){
	console.log(sValue);
}else{
	if(iNum == 0){
	for(var i=0; i<sValue.length; i=i+3){
		aValue[index] = sValue[i]+''+sValue[i+1]+''+sValue[i+2];
		index++;
	}
}else if(iNum == 1){
	aValue[0] = sValue[0];
	index = 1;
	for(var i=1; i<sValue.length; i=i+3){
		aValue[index] = sValue[i]+''+sValue[i+1]+''+sValue[i+2];
		index++;
	}
}else if(iNum == 2){
	aValue[0] = sValue[0]+''+sValue[1];
	index = 1;
	for(var i=2; i<sValue.length; i=i+3){
		aValue[index] = sValue[i]+''+sValue[i+1]+''+sValue[i+2];
		index++;
	}
}
aResult = aValue.join(',');
console.log(aResult.toString());// Output 20,002,365,879
}	

Related articles: