SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    685
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Remove Decimal from result?

    I have a script that is the following:

    $products = $_POST["products"] * .75;
    $items = $_POST["items"] * 3;
    $warehouse = $_POST["warehouse"] * .75;
    $outcome = $products + $items + $warehouse;
    echo $outcome;
    The outcome is a number. Sometimes the number is a repeating number such as

    75.666666666666666666666666667

    I'm wanting the result of $outcome to automatically remove all the numbers passed the decimal and put .95. Then remove the number just before the decimal (5 in the above example) and replace it with a 9.

    Does anyone know of an example to do this?

  2. #2
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't get it. How exactly do you want the result to loo

    Are you saying that you want the result to be 79.95 instead of 75.66666666666666666?

    You need to provide more details. I mean what if the result is 75.1? do you still want it to be 75.95? What if the result is 71.1?
    My project: Open source Q&A
    (similar to StackOverflow)
    powered by php+MongoDB
    Source on github, collaborators welcome!

  3. #3
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    685
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am saying I want the result to be 79.95

    If the result is 75.1 it would be 79.95

    If the result is 71.1 it would need to be changed to 79.95

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    echo floor($outcome 10) * 10 9.95;
    //or even just
    echo floor($outcome 10) . '9.95'
    Note the difference when $outcome is less than 10 though. If $outcome could be negative, there's more to think about.

  5. #5
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    685
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks! That worked like a charm


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
  •