What's the Best Way to Get the Fractional Part of a Float in PHP

What's the best way to get the fractional part of a float in PHP?

$x = $x - floor($x)

How to get whole and decimal part of a number?

$n = 1.25;
$whole = floor($n); // 1
$fraction = $n - $whole; // .25

Then compare against 1/4, 1/2, 3/4, etc.


In cases of negative numbers, use this:

function NumberBreakdown($number, $returnUnsigned = false)
{
$negative = 1;
if ($number < 0)
{
$negative = -1;
$number *= -1;
}

if ($returnUnsigned){
return array(
floor($number),
($number - floor($number))
);
}

return array(
floor($number) * $negative,
($number - floor($number)) * $negative
);
}

The $returnUnsigned stops it from making -1.25 in to -1 & -0.25

What would be the best way to detect if a float has a zero fraction value (e.g. 125.00) in PHP?

if($value == round($value))
{
//no decimal, go ahead and truncate.
}

This example compares the value to itself, rounded to 0 decimal places. If the value rounded is the same as the value, you've got no decimal fraction. Plain and simple.

How to split float in PHP?

A straight-forward approach - not very academic, but it works for PHP ;-):

$float        = 12.054;
$parts = explode('.', (string)$float);
$whole_number = $parts[0];
$numerator = trim($parts[1], '0');
$denominator = pow(10, strlen(rtrim($parts[1], '0')));

Some more work might be needed to ensure that edge case work too (trailing 0s, no decimal part at all, etc.).

How to get the exact fractional part from a floating point number as an integer?

The easiest way is to use standard library function ceil from <math.h>.

The float number 254.73 may be converted to 254.7299957275390625000000.

f-integer will give 0.7299957275390625000000.

Now multiply it by 100 and use ceil function to get the smallest integer value not less than 72.99957275390625000000.

int fractional_part_in_integer = ((int)ceil(fractional*100)) % 100;

UPDATE: As pointed in a comment by @Sneftel, the above suggested method in this answer will not work consistently.

A simple hack is to use round function from math.h to round the f and then extract the fractional part

float f=254.73;

int int_part = (int)f;
float fractional = round(f*100)/100 - int_part;
int fractional_part_in_integer = (int)(fractional*100);

printf("%d, %d\n ", int_part, fractional_part_in_integer);

Output:

254, 73

PHP check if number decimal is larger than .3

You can subtract floor($val) from $val to get the decimal value of $val. Eg:

if( $val - floor($val) >= 0.3  ) {
return true;
}

Note that it won't work if $val is negative, you can use abs for it to work:

if( abs($val) - floor(abs($val)) >= 0.3 ) {}

Or something like:

// if the negative number should be greater than 0.3
if( $val - floor($val) >= 0.3 && abs($val) - floor(abs($val)) >= 0.3 ) {}
// if the negative number should be less than 0.3
if( $val - floor($val) >= 0.3 && abs($val) - floor(abs($val)) <= 0.3 ) {}

How can I display a decimal number so that the integer part appears larger text size and the decimal part smaller

You need to explode the variable and just use basic HTML/CSS to style the different span's..

something like that

$string = '75.45 €';
echo '<span style="font-size: 1.5rem;">' . explode('.', $string)[0] . '.</span><span>' . explode('.', $string)[1] . '</span>';

C# get digits from float variable

The cheating way to do it is:

    private Int32 FractionalPart(double n)
{
string s = n.ToString("#.#########", System.Globalization.CultureInfo.InvariantCulture);
return Int32.Parse(s.Substring(s.IndexOf(".") + 1));
}

edit2: OK OK OK OK. Here is the most paranoid never fail version I can come up with. This will return the first 9 digits (or less, if there aren't that many) of the decimal portion of the floating point number. This is guaranteed to not overflow an Int32. We use the invariant culture so we know that we can use a period as the decimal separator.

PHP format decimal part of number

Don't know if there is a better solution but regex will do it.

$re = '/(\d{3})/'; // match three digits
$str = '0.000432532';
$subst = '$1 '; // substitute with the digits + a space

$result = preg_replace($re, $subst, $str);

echo $result;

https://regex101.com/r/xNcfq9/1

This has a limitation, the number can not be larger 99 or the integer part of the number will start to "break" up.

But it seems as you only use small numbers.



Related Topics



Leave a reply



Submit