[Solved] Split Float & Replace Integer PHP [closed]


<?php
    $number = 5.1234;
    $array = explode(".", $number);
    // $array[0] contains 5
    $newNumber = 8;
    $array[0] = $newNumber;
    $finalString = $array[0] . '.' . $array[1];
    $finalFloat = floatval($finalString); // String to float

    echo $finalFloat;

?>

Here is how I would do this. This solution is relevant if you are sure the number will always be formated like followed :

[number].[decimals]

Else you will not be able to always replace the number before the dot.

2

solved Split Float & Replace Integer PHP [closed]