Results 1 to 7 of 7

Thread: [RESOLVED] decimal format

  1. #1

    Thread Starter
    Frenzied Member met0555's Avatar
    Join Date
    Jul 2006
    Posts
    1,385

    Resolved [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 parseFloat($('#P_TextBox1').val());
                var 
    parseFloat($('#A_TextBox3').val());
                var 
    total_surch = ((100) * P);
                $(
    '#T_TextBox2').val(total_surch); 
    I would appriciate any tip on how to accomplish this.


    Thank You

  2. #2
    Hyperactive Member coothead's Avatar
    Join Date
    Oct 2007
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    284

    Re: decimal format

    Hi there met0555,

    have you tried...
    Code:
    
    var total_surch = ((A / 100) * P).toFixed(2);


    ~ the original bald headed old fart ~

  3. #3

    Thread Starter
    Frenzied Member met0555's Avatar
    Join Date
    Jul 2006
    Posts
    1,385

    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

  4. #4
    Hyperactive Member coothead's Avatar
    Join Date
    Oct 2007
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    284

    Re: decimal format

    Hi there met0555,

    try...
    Code:
    
    
    var total_surch = parseFloat(((A / 100) * P).toFixed(2));


    ~ the original bald headed old fart ~

  5. #5
    Hyperactive Member coothead's Avatar
    Join Date
    Oct 2007
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    284

    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 ~

  6. #6

    Thread Starter
    Frenzied Member met0555's Avatar
    Join Date
    Jul 2006
    Posts
    1,385

    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

  7. #7
    Hyperactive Member coothead's Avatar
    Join Date
    Oct 2007
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    284

    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
  •  



Click Here to Expand Forum to Full Width