Hi friends,
When we want to format, type cast and convert numbers in JAVASCRIPT with multilingual support, there are none. So came out with this.
This supports European & English both kind of number formatting. While using this, just set the basic 3 variables.
Save the code with any name, say NumberFormatter.js then call like this with jQuery
jQuery.getScript(
{
url: 'NumberFormatter.js',
dataType: "script",
success: function(){
NumberFormatter._tSep = '<get the data from user profile with server side code>>';
NumberFormatter._dSep = '<get the data from user profile with server side code>';
NumberFormatter.DEFAULT_PRECISION = <get the data from user profile with server side code>;
}
}
);
The code for Javascript file starts from here....
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ""); };
function NumberFormatter(){}
NumberFormatter._tSep = ',';
NumberFormatter._dSep = '.';
NumberFormatter.DEFAULT_PRECISION = 2;
NumberFormatter.IS_STRICT_THOUSAND_SEPARATOR_VALIDATION = false;
NumberFormatter.setThousandSeparator = function(tSep){
NumberFormatter._tSep = tSep;
}
NumberFormatter.setDecimalSeparator = function(dSep){
NumberFormatter._dSep = dSep;
}
NumberFormatter.getInteger = function(num2Get){
return NumberFormatter.parseNumber(num2Get, true);
}
NumberFormatter.getDecimal = function(num2Get, decimalPrecision){
return NumberFormatter.parseNumber(num2Get, false, decimalPrecision);
}
/**
* this function is to be used before sending
* to server side.
*/
NumberFormatter.parseNumber = function(num2Get, isNumberOnly, a_decimalPrecision){
if(num2Get == ""){
return "";
}
var tSep = NumberFormatter._tSep;
var dSep = NumberFormatter._dSep;
var parts = (num2Get + "").split(dSep);
var firstPart = parts[0];
var decPrec = (typeof(a_decimalPrecision) != "undefined" && !isNaN(a_decimalPrecision) && a_decimalPrecision >= 0) ? a_decimalPrecision : NumberFormatter.DEFAULT_PRECISION;
var secondPart = parts.length > 1 ? parts[1] : "00";
if(secondPart.trim() == ""){
secondPart = "00";
}
var tSepIndex = firstPart.indexOf(tSep);
while(tSepIndex >= 0){
firstPart = firstPart.replace(tSep, "");
tSepIndex = firstPart.indexOf(tSep, tSepIndex);
}
if(isNumberOnly){
return parseInt(firstPart);
}else{
if(isNaN(firstPart) || isNaN(secondPart))
{
return NaN;
}
//alert(Math.round(parseFloat(firstPart + "." + secondPart)*Math.pow(10,decPrec))/Math.pow(10,decPrec));
return Math.round(parseFloat(firstPart + "." + secondPart)*Math.pow(10, decPrec)) / Math.pow(10, decPrec);
}
}
function roundVal(val, decPrec){
var result = Math.round(val * Math.pow(10, decPrec)) / Math.pow(10, decPrec);
return result;
}
/**
* please do not use this, use the formatDecimal instead
*
* @deprecated
*/
NumberFormatter.formatNumber = function(num2Format){
return NumberFormatter.formatDecimal(num2Format, true);
}
NumberFormatter.formatDecimal = function(num2Format, isNumberOnly, a_decimalPrecision){
if(isNaN(num2Format)){
return num2Format;
}
if(!isFinite(num2Format)){
return num2Format;
}
if(typeof(num2Format) == 'number'){
num2Format = num2Format.toString();
}
if(num2Format == ""){
return "";
}
var tSep = NumberFormatter._tSep;
var dSep = NumberFormatter._dSep;
var decPrec = (typeof(a_decimalPrecision) != "undefined" && !isNaN(a_decimalPrecision) && a_decimalPrecision >= 0) ? a_decimalPrecision : NumberFormatter.DEFAULT_PRECISION;
var formattedNum = parseFloat(num2Format).toFixed(decPrec);
var isMinus = formattedNum < 0;
var parts = String(Math.abs(formattedNum)).split('.');
var firstPart = parts[0];
var secondPart = parts.length > 1 ? parts[1] + NumberFormatter.getLeading0s(decPrec - parts[1].length) : NumberFormatter.getLeading0s(decPrec);
if(firstPart.length <= 3){
if(isMinus){
firstPart = "-" + firstPart;
}
if(decPrec > 0 && !isNumberOnly) {
return (firstPart + dSep + secondPart);
}
return firstPart;
}
var newStr = "";
for(var i = firstPart.length - 1; i >= 0; i--){
newStr = firstPart.charAt(i) + newStr ;
if(i == firstPart.length - 1){
continue;
}
if(i == 0){
continue;
}
var d = (firstPart.length - i) / 3;
if(Math.ceil(d) == Math.floor(d)){
newStr = tSep + newStr;
}
}
if(isMinus){
newStr = "-" + newStr;
}
if(isNumberOnly){
return newStr;
}
return newStr + (decPrec > 0 ? (dSep + secondPart) : "");
}
NumberFormatter.validateInput = function(inputText, isNumberOnly, isToAllowNegetive, isMandatory, a_decimalPrecision){
if(typeof(inputText) == 'number'){
inputText = inputText.toString();
}
var returnObject = new Object();
var decPrec = (typeof(a_decimalPrecision) != "undefined" && !isNaN(a_decimalPrecision) && a_decimalPrecision >= 0) ? a_decimalPrecision : NumberFormatter.DEFAULT_PRECISION;
//lets test blank input
if(isMandatory && String(inputText).trim().length <= 0){
returnObject.message = "BLANK_INPUT";
returnObject.isValid = false;
return returnObject;
}
//no white space is allowed
if(inputText.indexOf(" ") >= 0){
returnObject.message = "WHITE_SPACE_IN_INPUT";
returnObject.isValid = false;
return returnObject;
}
//check valid chars
var ln = inputText.length;
for(var iCounter = 0; iCounter < ln; iCounter++){
var currentChar = inputText.charAt(iCounter);
//if the first char is minus then let it go
if(isToAllowNegetive && iCounter == 0 && currentChar == '-'){
continue;
}
if(isValidChar(currentChar) == false){
returnObject.message = "INVALID_CHARACTER_IN_INPUT";
returnObject.isValid = false;
return returnObject;
}
}
if(isNumberOnly && inputText.indexOf(NumberFormatter._dSep) >= 0){
returnObject.message = "DECIMAL_NOT_ALLOWED";
returnObject.isValid = false;
return returnObject;
}
var firstChar = inputText.charAt(0);
var lnToTest = inputText.split(NumberFormatter._tSep).join("").length;
if(firstChar == '-'){
lnToTest = lnToTest - 1;
}
if(lnToTest > 15){
returnObject.message = "TOO_LONG_INPUT";
returnObject.isValid = false;
return returnObject;
}
//check single decimal separator
var parts = inputText.split(NumberFormatter._dSep);
if(parts.length > 2){
returnObject.message = "MULTIPLE_DECIMAL_SEPARATOR";
returnObject.isValid = false;
return returnObject;
}
var firstPart = parts[0];
var secondPart = parts[1];
//in the decimal part there can not be thousand separator
if(secondPart!= null && secondPart.length > 0 && secondPart.indexOf(NumberFormatter._tSep) >= 0){
returnObject.message = "THOUSAND_SEPARATOR_IN_DECIMAL_PART";
returnObject.isValid = false;
return returnObject;
}
if(NumberFormatter.IS_STRICT_THOUSAND_SEPARATOR_VALIDATION){
var thousandParts = firstPart.split(NumberFormatter._tSep);
for(var iCounter = thousandParts.length - 1; iCounter > 0; iCounter--){
if(thousandParts[iCounter].length != 3){
returnObject.message = "INVALID_POSITIONING_OF_THOUSAND_SEPARATOR";
returnObject.isValid = false;
return returnObject;
}
}
}
/*if(secondPart.length > decPrec){
returnObject.message = "TOO_LONG_DECIMAL_PART";
returnObject.isValid = false;
return returnObject;
}*/
returnObject.message = "VALID_NUMBER";
returnObject.isValid = true;
return returnObject;
}
NumberFormatter.formatCurrency = function(num2Format){
return NumberFormatter.formatDecimal(num2Format);
}
NumberFormatter.getLeading0s = function(decPrec) {
var s0s=[];
for(var i = 0; i < decPrec; i++) {
s0s.push('0');
}
return s0s.join('');
}
function isValidChar(charToTest){
switch(charToTest){
case '0':
return true;
case '1':
return true;
case '2':
return true;
case '3':
return true;
case '4':
return true;
case '5':
return true;
case '6':
return true;
case '7':
return true;
case '8':
return true;
case '9':
return true;
case '.':
return true;
case ',':
return true;
default:
return false;
}
}
Hope this will help you all. So long till next time...