Root
Root

Reputation: 2349

how to remove more 1 decimal numbers on a number

What's the simple way to remove more than 1 decimal number from source number .
for example source numbers are :

1st source number is : 56.48216585224
2nd source number is: 93

Output must be :

1st output : 56.4
2nd output: 93

numbers are not static

what's the simple way ?

Upvotes: 0

Views: 78

Answers (3)

Baba
Baba

Reputation: 95131

You can try the following

print(quickFormat("56.48216585224"));
print(quickFormat("93"));


function quickFormat($number) {
    $number = explode(".", $number);
    if (isset($number[1])) {
        return $number[0] . "." . $number[1]{0};
    } else {
        return $number[0] ;
    }
}

Output

56.4
93

Upvotes: 0

anubhava
anubhava

Reputation: 785531

I will suggest this PHP code for your requirement:

$n = 56.48216585224;
$m = floor($n * 10) / 10; // $m = 56.4 now

Upvotes: 0

David
David

Reputation: 2065

If you don't want rounding, then:

$number = 56.48216585224;
echo substr($number, 0, strpos($number, '.')+2); // output: 56.4

Otherwise:

Use php round() or number_format()

http://php.net/manual/en/function.round.php

http://php.net/manual/en/function.number-format.php

Examples:

$number = 56.48216585224;
echo number_format($number, 1, '.', ''); // Output: 56.5
echo round($number, 1); // Output: 56.5

Upvotes: 3

Related Questions