473,387 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Quaterly rounding in PHP

alexphd
19
I had post a thread on rounding before, but it really wasnt what I needed. I need quaterly rounding I have tried this

$FormatFinalTotal = (ceil($FinalTotal*20))/20;

and that rounds 67.67 to 67.70 or 23.23 to 23.25


what i need is 60.10 to 60.25 or 67.67 to 67.75

or 67.80 to 68.00



Any help would greatly be appreciated.

Thanks,

Alex
May 20 '08 #1
9 3587
coolsti
310 100+
Try this:

Expand|Select|Wrap|Line Numbers
  1. $FormatFinalTotal = (round($FinalTotal*4))/4;
  2.  
Its all just mathematics.
May 20 '08 #2
hsriat
1,654 Expert 1GB
Try this:

Expand|Select|Wrap|Line Numbers
  1. $FormatFinalTotal = (round($FinalTotal*4))/4;
  2.  
Its all just mathematics.
You missed one thing though. It's ceil, not round.

And alexphd, you could have continued in the same thread rather then a new thread.
May 20 '08 #3
coolsti
310 100+
Hmmm, ceil or round depends on the intentions of the OP. It would be "round" if the OP wants to round the result to the nearest quarter, and "ceil" if the OP wants to round up to the nearest quarter.

Or?
May 20 '08 #4
hsriat
1,654 Expert 1GB
Look at his requirements, you will come to know about his intentions..

60.10 to 60.25 or 67.67 to 67.75 or 67.80 to 68.00
May 20 '08 #5
dlite922
1,584 Expert 1GB
Look at his requirements, you will come to know about his intentions..

60.10 to 60.25 or 67.67 to 67.75 or 67.80 to 68.00
I'm having a horrible case of Deja Vu

0_0

-Dan
May 21 '08 #6
alexphd
19
both answered helped out a lot.

I have a question though how did you know to divide by 4 and multiply by 4?

Alex
May 21 '08 #7
TheServant
1,168 Expert 1GB
both answered helped out a lot.

I have a question though how did you know to divide by 4 and multiply by 4?

Alex
It's an old mathematical trick. I learnt it at uni, but it is quite simple when you think about it! You can do it with normal rounding (use 2 instead of 4) etc...
May 21 '08 #8
hsriat
1,654 Expert 1GB
Well, I never used this trick. It just came across my mind when someone in JavaScript forum asked me to round a number to the nearest hundredth.
ie. 5.867 to 5.87, 3.653 to 3.65

There's no second parameter in JavaScript like in PHP which could define the number of digits. So I told him to first multiply it with 100, then round it and then divide it with 100 again.

As far as the usage of the multiple (4 or 20) is concerned, inverse your interval, you will get the required multiple.

eg. if your interval is 0.05, multiple is 1/0.5 = 20
interval = 0.25, multiple = 1/0.25 = 4 (quarterly rounding case)

And as far as ceil and round are concerned, ceil will return the upper limit, and round will return the nearest limit, whether upper or lower. floor will return the lower limit always.

Expand|Select|Wrap|Line Numbers
  1. Number      Ceil        Floor       Round
  2. 1.78         2           1           2 
  3. 1.50         2           1           2
  4. 1.49         2           1           1
  5. 1.10         2           1           1
Regards,
Harpreet
May 21 '08 #9
coolsti
310 100+
Hi Alexphd,

why did I know to use the factor 4? It just came to me, really. The keyword was that you wanted to round to the nearest "quarter", and in order to use the functions floor(), ceil() or round() you need to remove the "decimal" portion of the number for the whole quarters. And to do this, you need to multiply the number by 4. Then any "quarter", i.e. 0.0, 0.25, 0.5 and 0.75, will become a whole number without decimal. Anything else can then be rounded, rounded up, or rounded down as wished. But then to get back to the original number again you need to reverse the scaling by 4, meaning you need to then divide by 4.
May 21 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Norvin Laudon | last post by:
Hi, Can somebody explain the following, from the MSDN documentation for the "System.Convert.ToInt32(double)" function <quote> Return Value value rounded to the nearest 32-bit signed...
4
by: spebola | last post by:
I am using vb.net 2003 professional and I get the following results when using the round method: dim Amount as decimal = 180.255 Amount = Amount.Round(Amount, 2) Amount now contains 180.25. ...
8
by: Zorpiedoman | last post by:
Howcome: Dim D as decimal = .5D msgbox d.Round(D, 0) this returns "0" Now when I went to school .5 rounds UP to 1 not DOWN to zero?????!!! Documentation says this, but what the heck are...
2
by: Jiri Nemec | last post by:
Hello all, I have got one table with rounding values, table contains prices and round types. id price_from price_to rounding 1 0 1500 0.1 2 1500 ...
11
by: cj | last post by:
Lets assume all calculations are done with decimal data types so things are as precise as possible. When it comes to the final rounding to cut a check to pay dividends for example in VB rounding...
18
by: jdrott1 | last post by:
i'm trying to round my currency string to end in 9. it's for a pricing application. this is the function i'm using to get the item in currency: FormatCurrency(BoxCost, , , , TriState.True) if...
206
by: md | last post by:
Hi Does any body know, how to round a double value with a specific number of digits after the decimal points? A function like this: RoundMyDouble (double &value, short numberOfPrecisions) ...
20
by: jacob navia | last post by:
Hi "How can I round a number to x decimal places" ? This question keeps appearing. I would propose the following solution #include <float.h> #include <math.h>
30
by: bdsatish | last post by:
The built-in function round( ) will always "round up", that is 1.5 is rounded to 2.0 and 2.5 is rounded to 3.0. If I want to round to the nearest even, that is my_round(1.5) = 2 # As...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.