SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: text formatting

  1. #1
    SitePoint Zealot
    Join Date
    Jul 2001
    Location
    Grosse Pointe Farms, MI
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    text formatting

    Ok, so I have a dollar amount that is calculated on the page based on amounts entered into textboxes.

    The result tends to end up being something like 27.432

    how do I round to 2 decimal places for output?

    thanks,
    Ryan
    Michigan looks like your left hand . . . half way between your wrist and the base of your thumb is where I live.

  2. #2
    SitePoint Guru okrogius's Avatar
    Join Date
    Mar 2002
    Location
    US
    Posts
    622
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    http://sitepointforums.com/search.ph...by=&sortorder=

    Also take a look in the php manual for substr function.
    Last edited by Codename49; Jul 8, 2002 at 15:19.

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2002
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    var sum= your total

    // - this will round it to 2 decimal places
    sum=Math.round(sum*100)/100;

    // - the rest here makes sure your number displays
    // - at least 2 decimal places

    sum=sum.toString();
    var dot=sum.substring(sum.indexOf('.',0)+1,sum.length);
    if (sum.indexOf('.',0)==-1){sum=sum+'.00'}
    else if (dot.length==1){sum=sum+'0'}

  4. #4
    SitePoint Zealot
    Join Date
    Jul 2001
    Location
    Grosse Pointe Farms, MI
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks,

    did the trick.

    -Ryan
    Michigan looks like your left hand . . . half way between your wrist and the base of your thumb is where I live.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •