-
May 21st, 2013, 11:40 AM
#1
Thread Starter
Frenzied Member
[RESOLVED] decimal format
Hi,
I'm currently using the following code to computer a % , it is working fine, but I only want max two digits after the dot like this xxxxxxxxx.XX .
PHP Code:
var P = parseFloat($('#P_TextBox1').val()); var A = parseFloat($('#A_TextBox3').val()); var total_surch = ((A / 100) * P); $('#T_TextBox2').val(total_surch);
I would appriciate any tip on how to accomplish this.
Thank You
-
May 21st, 2013, 01:41 PM
#2
Re: decimal format
Hi there met0555,
have you tried...
Code:
var total_surch = ((A / 100) * P).toFixed(2);
~ the original bald headed old fart ~
-
May 21st, 2013, 02:38 PM
#3
Thread Starter
Frenzied Member
Re: decimal format
ok, I tried your suggestion, it worked but it also created another problem. Normally after running the code i posted, i will add all the results together
var final_total = (total_surch + total_money .....)
But now when i try to add up everything, it is treating it as a string rather than numeric values, so it is adding like this '0.1056.456.3' so basically '+' is same as '&' here, I guess i will need to fins a way to convert it back to a numeric format.
thx
-
May 21st, 2013, 02:50 PM
#4
Re: decimal format
Hi there met0555,
try...
Code:
var total_surch = parseFloat(((A / 100) * P).toFixed(2));
~ the original bald headed old fart ~
-
May 21st, 2013, 02:56 PM
#5
Re: decimal format
Hi there met0555,
note that the "toFixed()" function rounds up the number so that 68. 129 would become 68. 13.
If that is unacceptable, then try this...
Code:
var total_surch = ((A / 100) * P);
var S=total_surch.toString().split('.');
total_surch=parseFloat(S[0]+'.'+S[1].substr(0,2));
~ the original bald headed old fart ~
-
May 21st, 2013, 04:42 PM
#6
Thread Starter
Frenzied Member
Re: decimal format
Thanks for my first issue i used your suggestion, and for my second problem i just used
parseFloat(total_surch) + parseFloat(total_money .....)
Thanks
-
May 21st, 2013, 04:50 PM
#7
Re: decimal format
No problem, you're very welcome.
~ the original bald headed old fart ~
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|