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

getting fraction from decimal number

This isn't necessarily a php-specific question, but I'm looking for a
formula to determine the fraction portion of a decimal number. For instance,
8.25. I could simply search the number (as a string) and return everything
from the right of the decimal point (.25). However is there a formula to do
this more eloquently?

Thanks in advance.
Jul 17 '05 #1
2 2787
Bacci wrote:
This isn't necessarily a php-specific question, but I'm looking for a
formula to determine the fraction portion of a decimal number. For instance,
8.25. I could simply search the number (as a string) and return everything
from the right of the decimal point (.25). However is there a formula to do
this more eloquently?


Maybe

<?php
$x = 8.25;
$y = $x - (int)$x; // $y now has 0.25
?>
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #2
Regarding this well-known quote, often attributed to Pedro Graca's famous
"8 Feb 2004 12:20:43 GMT" speech:
Bacci wrote:
This isn't necessarily a php-specific question, but I'm looking for a
formula to determine the fraction portion of a decimal number. For instance,
8.25. I could simply search the number (as a string) and return everything
from the right of the decimal point (.25). However is there a formula to do
this more eloquently?


Maybe

<?php
$x = 8.25;
$y = $x - (int)$x; // $y now has 0.25
?>


I'd recommend using:
$y = $x - floor($x);
to be sure of the rounding method.

--
-- Rudy Fleminger
-- sp@mmers.and.evil.ones.will.bow-down-to.us
(put "Hey!" in the Subject line for priority processing!)
-- http://www.pixelsaredead.com
Jul 17 '05 #3

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

Similar topics

0
by: Bacci | last post by:
This isn't necessarily a php-specific question, but I'm looking for a formula to determine the fraction portion of a decimal number. For instance, 8.25. I could simply search the number (as a...
8
by: Schklerg | last post by:
I have this script performing a calculation on a page on my site: <script language="JavaScript"> function compute_weight(form) { var weight = form.wt.value; var pgs = form.pgs.value; var ppi =...
9
by: John Cho | last post by:
// CHO, JOHN #include<iostream> class fracpri{ int whole; int numer; int denom;
13
by: Steve | last post by:
I am having trouble finding the answer to this question, I'm thinking the solution must be blindingly obvious, so therefore of course I cannot see it. I wish to take a fraction in DEC say .4 and...
4
by: M. Uppal | last post by:
How do i convert fraction to time in c#. I need to convert 0.25 to time. Excel does this by using Selection.NumberFormat = "h:mm:ss;@" of the Cell Format function. thanks, M. Uppal
9
by: arun.hallan | last post by:
I need to derive fractions from decimals as so: 0.3333 = 1/3 0.6666 = 2/3 The decimal could also be 0.33, or 0.3333333 but the point is that it that the fraction needs to be extracted. ...
5
by: Jeff Dillon | last post by:
How might I convert a string like 10.A (in hex) to it's decimal equivalent? Basically I have an input string like ((1F.A + 3A.D) - 1F.E) and need to calculate the result. Using Reflection and...
19
by: bing3121 | last post by:
Hello, The following C++ code is what I am trying to use in a fraction class to convert a fraction to a decimal. the num and den are both ints. When I make a call to the function...
17
by: =?Utf-8?B?TWljaGVsIFBvc3NldGggW01DUF0=?= | last post by:
Hello , Does someone knows a simple way of how to get the nr of fraction digits ? example : 1.23 would give a result of 2 1.234 would give a result of 3 Yes
135
by: robinsiebler | last post by:
I've never had any call to use floating point numbers and now that I want to, I can't! *** Python 2.5.1 (r251:54863, May 1 2007, 17:47:05) on win32. *** 0.29999999999999999 0.29999999999999999
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.