473,386 Members | 1,753 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,386 software developers and data experts.

reducing decimals

Hello,

Doing a simple division, I obtain this number :
0.002409638554216867234458732127677649259567260742 1875
I'd like to have 0.0024 instead.
I know the number_format function but is it the right solution ?
I thought of another solution :

$MyDec = 0.002409638554216867234458732127677649259567260742 1875;
$MyDecOk = round($MyDec * 10000) / 10000 ; # =0.0024

Is there a special function to do so ?

Thanks in advance,

Albert
Aug 18 '07 #1
5 2186
..oO(albert)
>Doing a simple division, I obtain this number :
0.00240963855421686723445873212767764925956726074 21875
I'd like to have 0.0024 instead.
I know the number_format function but is it the right solution ?
I thought of another solution :

$MyDec = 0.002409638554216867234458732127677649259567260742 1875;
$MyDecOk = round($MyDec * 10000) / 10000 ; # =0.0024

Is there a special function to do so ?
Have a look at round()'s second parameter ...

Micha
Aug 18 '07 #2
Michael Fesser wrote:
>>Is there a special function to do so ?

Have a look at round()'s second parameter ...
format_number() and sprintf() are also useful, if you just want to hide the
extra decimals when echo()ing the numbers.

--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

Orilla: período de tiempo de 60 minutillos.
Aug 18 '07 #3
Have a look at round()'s second parameter ...

I tried this :

$MyDec = 0.002409638554216867234458732127677649259567260742 1875;
$MyDecOk = round($MyDec, 4) ;

The trouble is that this data is serialized in a text file afterward.
Even with round($MyDec, 4), I do get 0.0023999999999... in the text file !
I used the number_format function instead.

albert

Aug 20 '07 #4
albert wrote:
>Have a look at round()'s second parameter ...

I tried this :

$MyDec = 0.002409638554216867234458732127677649259567260742 1875;
$MyDecOk = round($MyDec, 4) ;

The trouble is that this data is serialized in a text file afterward.
Even with round($MyDec, 4), I do get 0.0023999999999... in the text file !
I used the number_format function instead.

albert
Yep, that's what happens with floating point numbers. Floating point is
stored in base 2, not base 10. 0.0024 in this format is a repeating
number (similar to 1/3 in decimal) and cannot be represented directly.

But now you're saying that you're storing the number in a text file -
which you didn't before. That's new information. Albert's comment is
right on with the original information you gave. With the new
information, number_format should work fine.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Aug 20 '07 #5
On Aug 20, 8:18 am, Jerry Stuckle <jstuck...@attglobal.netwrote:
albert wrote:
Have a look at round()'s second parameter ...
I tried this :
$MyDec = 0.002409638554216867234458732127677649259567260742 1875;
$MyDecOk = round($MyDec, 4) ;
The trouble is that this data is serialized in a text file afterward.
Even with round($MyDec, 4), I do get 0.0023999999999... in the text file !
I used the number_format function instead.
albert

Yep, that's what happens with floating point numbers. Floating point is
stored in base 2, not base 10. 0.0024 in this format is a repeating
number (similar to 1/3 in decimal) and cannot be represented directly.

But now you're saying that you're storing the number in a text file -
which you didn't before. That's new information. Albert's comment is
right on with the original information you gave. With the new
information, number_format should work fine.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstuck...@attglobal.net
==================- Hide quoted text -

- Show quoted text -
Albert:

Try with the BCDIV() function, with 3 parameters: $number_1,$number_2
and $number_of_decimal

More information at....
http://cl.php.net/manual/en/ref.bc.php
http://cl.php.net/manual/en/function.bcdiv.php

Felipe Silva.

Aug 21 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Oswald | last post by:
Hey, how can I set the number of decimals? Example: 5 decimals 2.12345 2 decimals 2.12 I've found something about NumberFormat in the API, but without an example I can't solve this...
2
by: westjon64 | last post by:
i am fairly new to c++, i need to know how to make it so you can input decimals into a file using the cin >> a command instead of just being able to input whole numbers and also how to make it so...
6
by: Donal McWeeney | last post by:
Hi, Is there a way to specify on the predefined format strings like P and N that you want displayed all the decimals in the number... for example 3 will display 3 2.1 will display 2.1...
13
by: vsts2007 | last post by:
hi, how can I change the number of decimals appeared in access reports... I put a formula in query and its giving three to four decimals in the output of report. I want to change the number of...
12
by: CNiall | last post by:
I am very new to Python (I started learning it just yesterday), but I have encountered a problem. I want to make a simple script that calculates the n-th root of a given number (e.g. 4th root of...
0
by: Edwin.Madari | last post by:
for nth square root: use math.sqrt n times for example ... num = math.sqrt(num) ... 5.0 all comparisons work fine for arbitrary floating point numbers... For readability print them with...
0
by: Tommy Nordgren | last post by:
On 3 aug 2008, at 17.16, Edwin.Madari@VerizonWireless.com wrote: Ehum. The OP wants to compute the nth root ( not the nth square root) ------ What is a woman that you forsake her, and the...
38
by: ssecorp | last post by:
char* reverse(char* str) { int length = strlen(str); char* acc; int i; for (i=0; i<=length-1; i++){ acc = str; } return acc; }
8
by: =?Utf-8?B?a2FyaW0=?= | last post by:
Hello All, why is this code is not showing the result in this format: 0.00 and showing it as only 0 Private Sub btn1_Click Debug.Print(Format$(Rnd() * 100, "0.00")) Dim d As Double =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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.