You are here: irt.org | FAQ | JavaScript | Number | Q6 [ previous next ]
<script language="JavaScript"><!--
function cent(amount) {
// returns the amount in the .99 format
amount -= 0;
return (amount == Math.floor(amount)) ? amount + '.00' : ( (amount*10 == Math.floor(amount*10)) ? amount + '0' : amount);
}
document.write(cent(0)+'<br>');
document.write(cent(1)+'<br>');
document.write(cent(1.00)+'<br>');
document.write(cent(.5)+'<br>');
document.write(cent(.99)+'<br>');
document.write(cent('5.50')+'<br>');
//--></script>The following was submitted by David Parenteau:
This function is more efficient. It rounds the amount and checks if it is NaN (Not a Number):
<SCRIPT LANGUAGE="JavaScript"><!--
function cent(amount) {
// returns the amount in the .99 format
amount -= 0;
amount = (Math.round(amount*100))/100;
return (amount == Math.floor(amount)) ? amount + '.00' : ( (amount*10 == Math.floor(amount*10)) ? amount + '0' : amount);
}
alert(isNaN(cent('a')));
alert(cent('001'));
alert(cent('1.7'));
alert(cent('1.444'));
//--></script>The following (by Michel Plungjan) is a most robust solution, it will validate an amount AND set variables with:
It can handle european numbers (so both 1,000.00 and 1.000,00). The script does not round so 1,000.111 will be an invalid amount
<script language="JavaScript"><!--
isMoneyFormatAmount = 0.00;
isMoneyFormatString = "0.00";
function isMoneyFormat(str,eur) {
isMoneyFormatAmount = 0.00;
isMoneyFormatString = "0.00";
if(!str) return false;
str = "" + str; // force string
for (var i=0; i<str.length;i++) {
var ch = str.charAt(i);
if (!isNum(ch) && ch!='.' && ch != ',' && ch !='-') return false
}
var sign = 1;
var signChar = '';
isMoneyFormatAmount = 0.00;
isMoneyFormatString = "0.00";
if (str.length > 1) {
signChar = str.substring(0,1);
if (signChar == '-' || signChar == '+' ) {
if (signChar == '-') sign = -1;
str = str.substring(1);
}
else signChar = '';
}
var decimalPoint = '.';
var thDelim = ',';
if (eur) {
decimalPoint = ','
thDelim = '.';
}
test1 = str.split(decimalPoint);
if (test1.length == 2) { // Decimals found
if (test1[1].length > 2) return false; // more than 2 decimals
if (isNum(test1[1])) {
if (test1[1] < 9 && test1[1].charAt(0) > 0) test1[1] = new String(test1[1]+"0");
}
else return false;
}
else test1[1] = "00"; // force decimals
if (test1[0] == '') test1[0] = 0;
if (test1[0] && test1[0].indexOf(thDelim) != -1) {
test2 = test1[0].split(thDelim);
if (test2.length >= 2) { // thousands found
var thError = false;
for (var i=0;i<test2.length;i++) {
if (test2[i].length < 3 && i != 0) { thError = true; break; } // all thousands exept the first.
if (!isNum(test2[i])) { thError = true; break; } // all numbers
}
if (thError) return false;
test1[0] = test2.join('')
}
}
isMoneyFormatAmount = (parseInt(test1[0]) + parseFloat('.'+test1[1]))*sign;
isMoneyFormatString = new String(""+signChar+""+parseInt(test1[0])) +'.'+test1[1];
return true;
}
function isNum(str) {
if(!str) return false;
for(var i=0; i<str.length; i++){
var ch=str.charAt(i);
if ("0123456789".indexOf(ch) ==-1) return false;
}
return true;
}
document.write('<br><b>1,234.50:<\/b> ')
document.write(isMoneyFormat("1,234.50") + ' -> ' + isMoneyFormatAmount + '(' + isMoneyFormatString+')<hr>');
document.write('<br><b>1.234,50 (eur):<\/b> ')
document.write(isMoneyFormat("1.234,50",1) + ' -> ' + isMoneyFormatAmount + '(' + isMoneyFormatString+')<hr>');
document.write('<br><b>-1,234.50:<\/b> ')
document.write(isMoneyFormat("-1,234.50") + ' -> ' + isMoneyFormatAmount + '(' + isMoneyFormatString+')<hr>');
document.write('<br><b>1,1234.50:<\/b> ')
document.write(isMoneyFormat("1,1234.50",1) + ' -> ' + isMoneyFormatAmount + '(' + isMoneyFormatString+')<hr>');
document.write('<br><b>1.1234,50 (eur):<\/b> ')
document.write(isMoneyFormat("1.1234,50",1) + ' -> ' + isMoneyFormatAmount + '(' + isMoneyFormatString+')<hr>');
document.write('<br><b>50:<\/b> ')
document.write(isMoneyFormat(".50") + ' -> ' + isMoneyFormatAmount + '(' + isMoneyFormatString+')<hr>');
document.write('<br><b>.5:<\/b> ')
document.write(isMoneyFormat(".5") + ' -> ' + isMoneyFormatAmount + '(' + isMoneyFormatString+')<hr>');
document.write('<br><b>1a3:<\/b> ')
document.write(isMoneyFormat("1a3") + ' -> ' + isMoneyFormatAmount + '(' + isMoneyFormatString+')<hr>');
//--></script>