Solved

How to remove decimals from calculation amount

Posted on 2016-11-06
8
45 Views
Last Modified: 2016-11-06
I have a calculation like this (might be an easier way to work this out, but I am no maths boffin)

$discount_percent = ($reg_price-$sale_price)/$reg_price*100;

Open in new window


This sometimes gives me something like 41.3333333333% which is obviously not what I want to display.

How would you remove all the .3 so it just shows 41% instead.

the best would be that if it is 41.5% or up to 41.9% it would round up to 42% and round down to 41.5% if less than 41.5%
0
Comment
Question by:Black Sulfur
  • 4
  • 3
8 Comments
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 350 total points
ID: 41876100
number_format() is your friend
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 41876101
Use round() function
$discount_percent = round(($reg_price-$sale_price)/$reg_price*100);

Open in new window

0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 41876102
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 31

Assisted Solution

by:Marco Gasi
Marco Gasi earned 150 total points
ID: 41876103
Okay, number_format() affects what you display, round affects the result of the math op :)
1
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 41876104
Marco: Exactly Right.  In my experience, the numeric values used in computations don't really need to be messed with - it's just the display that is the issue.  This kind of goes to the Model-View-Controller philosophy.  The Model carries the true  value (maybe including fractional cents) and the View presents a sensible "human" picture representation of the value.
0
 
LVL 1

Author Comment

by:Black Sulfur
ID: 41876105
Man, you guys on Experts Exchange are the best! number_format() did the trick :)
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 41876107
Ray, you're always a step forward :)
1
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 41876196
Thanks for your kind words!

I would just add that when comparing floating point numbers, especially when looking for equality, you have an inherent risk in PHP.  See these links for details:
http://php.net/manual/en/language.operators.comparison.php
http://php.net/manual/en/language.types.float.php

One way to handle that risk is to use number_format() to about 6 decimals and compare the resulting strings.  Another is to learn about the "machine epsilon" and use that strategy.
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question