PDA

View Full Version : Currency Formatting for 1000 separators including decimals



gsrao27
05-30-2008, 10:12 AM
Hi,

How i need to perform currency formatting including decimal values, and it should not take invalid characters including +/- symbols and i can able to specify any number of decimals
based on that it should display the currency format

if i give as 123456 then the result should be 123456.00
if i give as 123456.123 then the result should be 123,456.123
if i give as 123456789.12345 then result should be 123,456,789.12345

and should be in onblur only

How can i do this can anybody help me

Thanks&regards,
latha Rao,

timeblue
05-30-2008, 02:38 PM
I think String class and Array class will meet your requirement.

jscheuer1
05-30-2008, 02:46 PM
I'm not sure how you want to deal with the +/- sign. It could be stripped, preserved, or rejected. Here it's rejected:


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function formatThous(num){
var f = formatThous;
if (isNaN(parseFloat(num)) || (f.rejectSign && /(\+)|(-)/.test(num.toString(10))) || num.toString(10).split('.').length > 2){
alert (f.errorMessage);
return num;
}
var l, c = 1, t = '',
sign = /^(\+)|(-)/.test(num.toString(10).charAt(0)) && !f.stripSign? num.toString(10).charAt(0) : '';
num = num.toString(10).replace(/[^\d\.]/g, '').split('.');
l = num[0].length-1;
while (l + 1){
t = c%3||!l? num[0].charAt(l) + t : ',' + num[0].charAt(l) + t;
l--;
c++;
}
return sign + t + (num[1] && num[1].length? '.' + num[1] : '');
}
// Configuration:
formatThous.errorMessage = 'Numerals Only With a Maximum of One Decimal Point Symbol Only, Please.'; // Message to Alert onerror
formatThous.rejectSign = true; // set to true to reject attempts use +/- signs, false to either strip or preserve these signs
formatThous.stripSign = true; // requires rejectSign = false, set to true to strip these signs, false to preserve them
</script>
</head>
<body>
<input type="text" onblur="this.value = formatThous(this.value);">
</body>
</html>

But I've left in the code that (with a few minor configuration changes) could be used to either strip or to preserve a leading + or - symbol.

gsrao27
05-31-2008, 06:26 AM
Hi John,

Thanks a lot for helping the code is working fine

but if i give the value as 12345 then it should display as 12,345.00

and also if i do not give any value or if the textfield is empty it should not throw any error message



Can u help me as i am a new for javascript



thanks
Latha Rao

jscheuer1
05-31-2008, 07:06 AM
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function formatThous(num){
var f = formatThous;
if (!num) return '';
if (isNaN(parseFloat(num)) || (f.rejectSign && /(\+)|(-)/.test(num.toString(10))) || num.toString(10).split('.').length > 2){
alert (f.errorMessage);
return num;
}
var l, c = 1, t = '',
sign = /^(\+)|(-)/.test(num.toString(10).charAt(0)) && !f.stripSign? num.toString(10).charAt(0) : '';
num = num.toString(10).replace(/[^\d\.]/g, '').split('.');
l = num[0].length-1;
while (l + 1){
t = c%3||!l? num[0].charAt(l) + t : ',' + num[0].charAt(l) + t;
l--;
c++;
}
return sign + t + (num[1] && num[1].length >= 2? '.' + num[1] : num[1] && num[1].length == 1? '.' + num[1] + '0' : '.00');
}
// Configuration:
formatThous.errorMessage = 'Numerals Only With a Maximum of One Decimal Point Symbol Only, Please.'; // Message to Alert onerror
formatThous.rejectSign = true; // set to true to reject attempts use +/- signs, false to either strip or preserve these signs
formatThous.stripSign = true; // requires rejectSign = false, set to true to strip these signs, false to preserve them
</script>
</head>
<body>
<input type="text" onblur="this.value = formatThous(this.value);">
</body>
</html>

gsrao27
05-31-2008, 08:03 AM
Hi John,

You helped me alot in solving my big problem

I need some restriction to provide for decimals

if user wants only 4 decimals it should accept only 4 decimals if i give more than the given decimals it should throw an error message that
"You should not exceed the given decimal digits!!!".

jscheuer1
05-31-2008, 08:17 AM
Well, before we do that, how about rounding any numbers with more than 4 decimal places to 4 decimal places instead? I don't see any great need to scold the user for using more than 4, and they will quickly get the idea once they see what happens if they use more than 4.

gsrao27
05-31-2008, 08:33 AM
Hi John,


as per your reply, there should not be rounded if i give as 12345.12345 then
it should be display 123,45.12345


the format of calling the function should be like


formatThous(num,decimalValues) {
..

if(num[1].length > decimalValues) {
alert("Invalid data: should not exceed "+decimalValues +"!!");
}

jscheuer1
05-31-2008, 03:14 PM
You like making your users mad, huh? (In email, often anything with two exclamation points [!!] is picked up by 'flame' filters.) The more the chance of 'being scolded' by a form (politely or otherwise), the less likely people will be to continue using it. A user friendly form will self correct where possible. Very well though, I at least toned down the language in the alert a little:


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function formatThous(num, places){
var f = formatThous;
if (!num) return '';
else if (isNaN(parseFloat(num)) || (f.rejectSign && /(\+)|(-)/.test(num.toString(10))) || num.toString(10).split('.').length > 2){
alert (f.errorMessage);
return num;
}
else if (num.toString(10).split('.').length > 1 && num.toString(10).split('.')[1].length > places){
alert ('Invalid Data: Do Not Exceed ' + places + ' Decimal Places, Please.');
return num;
}
var l, c = 1, t = '',
sign = /^(\+)|(-)/.test(num.toString(10).charAt(0)) && !f.stripSign? num.toString(10).charAt(0) : '';
num = num.toString(10).replace(/[^\d\.]/g, '').split('.');
l = num[0].length-1;
while (l + 1){
t = c%3||!l? num[0].charAt(l) + t : ',' + num[0].charAt(l) + t;
l--;
c++;
}
return sign + t + (num[1] && num[1].length >= 2? '.' + num[1] : num[1] && num[1].length == 1? '.' + num[1] + '0' : '.00');
}
// Configuration:
formatThous.errorMessage = 'Numerals Only With a Maximum of One Decimal Point Symbol Only, Please.'; // Message to Alert onerror
formatThous.rejectSign = true; // set to true to reject attempts use +/- signs, false to either strip or preserve these signs
formatThous.stripSign = true; // requires rejectSign = false, set to true to strip these signs, false to preserve them
</script>
</head>
<body>
<input type="text" onblur="this.value = formatThous(this.value, 4);">
</body>
</html>

If there are any more changes you would like, please make a complete list instead of asking for them one at a time.

gsrao27
06-03-2008, 12:31 PM
............

gsrao27
06-03-2008, 12:35 PM
Hi John,

Small clarification regarding the currency formatting, currently i am using
Internet Explorer 6 , but what about other browsers if the user disables the javascript in
Internet explorer or if the user uses FireFox/any browser then ?
if the above function is compatible with all the browsers if not what sought of changes
i need to make

Please, clarify

Thanks,
Latha Rao