Alin Adrian
Alin Adrian

Reputation: 13

Get the number before dot symbol

How to get the number before dot symbol, i try using round() but sometimes give me wrong number for what i need, i have the following calculation, lets say

$Calc = 10/3;
$Calc = 3.333333333333333;

I'm trying to use round() php function

$Calc = 10/3;
$Calc = round(3.333333333333333);
$Calc = 3;

but for example if my result is 3.51+

$Calc = round(3.51);
$Calc = 4;

BUT i just need the 3, not the rest , how can i get that?

Upvotes: 0

Views: 2722

Answers (3)

Code Lღver
Code Lღver

Reputation: 15593

Use this code:

$Calc = 10/3;
$pos = strpos($Calc, '.');
$Calc = substr($Calc, 0, $pos);

This will give the all the digits before the decimal.

Upvotes: 0

overals
overals

Reputation: 92

Use floor:

echo floor(3.99); // 3
echo floor(3.023);// 3
echo floor(4.4);  // 4
echo floor(9.0);  // 9 

Upvotes: 0

Ram Sharma
Ram Sharma

Reputation: 8819

you could try with intval() function

echo intval(3.51); // output will be 3
echo intval(10/3); // output will be 3

Upvotes: 5

Related Questions