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
Solved

How to remove decimals from calculation amount

Posted on 2016-11-06
8
40 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 109

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 109

Expert Comment

by:Ray Paseur
ID: 41876102
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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 109

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
 

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 109

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

856 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