Login    Forum    Search    FAQ   Blog

Board index » Client-Side Development » Javascript




 Page 1 of 1 [ 2 posts ] 



Author Message
 Post subject: Number formatting in javascript
 Post Posted: Wed Aug 05, 2009 9:24 am 
Offline

Joined: Sat Feb 21, 2009 2:57 pm
Posts: 67
i have 3 Types of number Formats..Like 1234,1234.00,(1234) ..

Suppose i have 3 TextBoxes..

1st TextBox have 1234

2nd one-> 1234.00

3 rd one -> (1234)

when i enter what ever values in these TextBoxes i want according to the TextBoxes formattas..

Suppose when i type in first textbox 1234.5 i want to show only 1234..


Please help any body


Thanks


Top 
 Post subject: Re: Number formatting in javascript
 Post Posted: Wed Aug 05, 2009 9:25 am 
Offline

Joined: Sat Feb 21, 2009 11:06 am
Posts: 1794
Hi,

Code:
function format_number(pnumber,decimals){
if (isNaN(pnumber)) { return 0};
if (pnumber=='') { return 0};
var snum = new String(pnumber);
var sec = snum.split('.');
var whole = parseFloat(sec[0]);
var result = '';
if(sec.length > 1){
var dec = new String(sec[1]);
dec = String(parseFloat(sec[1])/Math.pow(10,(dec.length - decimals)));
dec = String(whole + Math.round(parseFloat(dec))/Math.pow(10,decimals));
var dot = dec.indexOf('.');
if(dot == -1){
dec += '.';
dot = dec.indexOf('.');
}
while(dec.length <= dot + decimals) { dec += '0'; }
result = dec;
} else{
var dot;
var dec = new String(whole);
dec += '.';
dot = dec.indexOf('.');
while(dec.length <= dot + decimals) { dec += '0'; }
result = dec;
}
return result;
}


answered by kranthi


Top 
Display posts from previous:  Sort by  
 
 Page 1 of 1 [ 2 posts ] 




Board index » Client-Side Development » Javascript


Who is online

Users browsing this forum: No registered users and 1 guest

 
 

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron