?
Solved

How to remove decimals from calculation amount

Posted on 2016-11-06
8
Medium Priority
?
55 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

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…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

770 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