Solved

How to remove decimals from calculation amount

Posted on 2016-11-06
8
22 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 108

Accepted Solution

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

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 108

Expert Comment

by:Ray Paseur
ID: 41876102
0
 
LVL 30

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
Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 108

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 30

Expert Comment

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
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.
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…
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 …

705 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now