SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    Scary's On The Wall
    Join Date
    Apr 2003
    Location
    PA
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cents -> Dollars

    All I need is a simple script that will format a number value (in cents) to a dollar format.

    Examples:

    599 -> 5.99
    2000 -> 20.00
    50 -> 0.50
    1 -> 0.01

    Thanks in advance!

  2. #2
    SitePoint Zealot Arkkimaagi's Avatar
    Join Date
    Mar 2002
    Location
    Lappeenranta, Finland
    Posts
    176
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, here's something, its ugly, but I think it works.
    Code:
    <script>
    
    function cents(amount){
      amount = amount/100;
      if (Math.floor(amount)==amount){
    	amount=amount+".00";
      }
      else if (Math.floor(amount*10)==amount*10){
    	amount=amount+"0";
      }
      return amount;
    }
    
    alert(cents(100))
    </script>

  3. #3
    Scary's On The Wall
    Join Date
    Apr 2003
    Location
    PA
    Posts
    518
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks much


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
  •