Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to remove decimals from calculation amount

Posted on 2016-11-06
8
Medium Priority
?
57 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1400 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 111

Expert Comment

by:Ray Paseur
ID: 41876102
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 31

Assisted Solution

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

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 111

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

Independent Software Vendors: 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!

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

604 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