How to get a fractional part of a number?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rashed
Forum Newbie
Posts: 17
Joined: Fri Jun 06, 2003 6:27 am
Location: Islamabad, Pakistan.

How to get a fractional part of a number?

Post by rashed »

How to get fractional part of a number?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Could you please elaborate on what you are trying to do. Give an example of a number you want to manipulate and what you want to manipulate it to. It will help us help you if you are more explicit about what you are trying to achieve.

Mac
rashed
Forum Newbie
Posts: 17
Joined: Fri Jun 06, 2003 6:27 am
Location: Islamabad, Pakistan.

Post by rashed »

Actually, i have to round off the value
like 1.50 becomes 2 and 1.49 remains at 1

I have to calculate percentage based upon this. i have seen ceil(),floor(),round(). If you please help me
Seattlebadboy
Forum Newbie
Posts: 11
Joined: Mon Jun 16, 2003 3:55 am
Location: Seattle, WA
Contact:

Post by Seattlebadboy »

That pretty simple.

Here is how it might look:

$value_1 = 1.50;
$value_1 = round( $value_1 );
echo $value_1;
display: 2


$value_1 = 1.49;
$value_1 = round( $value_1 );
echo $value_1;
display: 1
rashed
Forum Newbie
Posts: 17
Joined: Fri Jun 06, 2003 6:27 am
Location: Islamabad, Pakistan.

Post by rashed »

but how to get the number after the decimal fraction
like in 1.45 .45 how to get this?
Seattlebadboy
Forum Newbie
Posts: 11
Joined: Mon Jun 16, 2003 3:55 am
Location: Seattle, WA
Contact:

Post by Seattlebadboy »

Well let's say it's always a 2 digit number after the decimal. Like money. You could do:

// no decimal
$value_1 = 1.43;
$value_1 = substr( $value_1, -2);
echo $value_1;
// displays: 43

// in case you need the decimal
$value_1 = 1.43;
$value_1 = substr( $value_1, -3);
echo $value_1;
// displays: .43
rashed
Forum Newbie
Posts: 17
Joined: Fri Jun 06, 2003 6:27 am
Location: Islamabad, Pakistan.

Post by rashed »

thank you
Seattlebadboy
Forum Newbie
Posts: 11
Joined: Mon Jun 16, 2003 3:55 am
Location: Seattle, WA
Contact:

Post by Seattlebadboy »

Welcome
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Something else you can use to split up a number is explode():

Code: Select all

$numbers = array(4.23, 4.5, 3.583, 5);
foreach ($numbers as $number) {
	$num_info = explode('.', $number);
	if (!empty($num_info[1])) {
		echo 'fraction = '.$num_info[1];
	} else {
		echo 'fraction = 0';
	}
	echo '<br />';
}
Mac
bjg
Forum Newbie
Posts: 15
Joined: Tue Jun 10, 2003 9:42 am
Location: .au

Post by bjg »

rashed wrote:but how to get the number after the decimal fraction
like in 1.45 .45 how to get this?

Code: Select all

$num = '1.45';
print substr(strstr($num, '.'), 1); // 45
print strstr($num, '.'); // .45
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Seattlebadboy - could you cut down on the widht of that avatar of yours, please?

Cheers :)
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

patrikG wrote:Seattlebadboy - could you cut down on the widht of that avatar of yours, please?

Cheers :)
I kinda took action myself (sent you a PM Seattlebadboy). Seems like the forum decided not to stick to the preset avatar limits.

Mac
rashed
Forum Newbie
Posts: 17
Joined: Fri Jun 06, 2003 6:27 am
Location: Islamabad, Pakistan.

Post by rashed »

Thank you Mac but i think foreach loop takes much CPU cycles than simple conversion from one type to another.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

rashed wrote:Thank you Mac but i think foreach loop takes much CPU cycles than simple conversion from one type to another.
The foreach() was just used in the example to show the results from a bunch of different number formats.

Mac
Post Reply