Modalità di arrotondamento a due cifre decimali PHP
-
27-10-2019 - |
Domanda
Vorrei arrotondare un numero a due cifre decimali in PHP.
Codice PHP:
$bmi = ($form_state[values][submitted][1] * 703) /
($form_state[values][submitted][10] * $form_state[values][submitted][10]);
$form_values['submitted'][11] = $bmi;
$form_values['submitted_tree'][11] = $bmi;
Qual è il modo migliore per arrotondare la variabile $bmi
?
Soluzione
Edit3:
Beh, se ho capito il tuo codice dovrebbe essere simile a questo:
<?php
//Calculate $bmi
$bmi = ($form_state[values][submitted][1] * 703) /
($form_state[values][submitted][10] * $form_state[values][submitted][10]);
//Fill unrounded $bmi var into array for whatever reason
$form_values['submitted'][11] = $bmi;
//Fill unrounded $bmi var into array for whatever reason
$form_values['submitted_tree'][11] = $bmi;
//$bmi contains for example 24.332423
$bmi = round($bmi,2);
//Should output 24.33
echo $bmi;
?>
Se qualcosa va storto posso solo supporre che il calcolati $bmi var si incasinato da qualche parte.
Si suppone che si riempie la unrounded valore in $form_values['presentato'][11]?se non provare il seguente:
$bmi = ($form_state[values][submitted][1] * 703) / ($form_state[values][submitted][10] *
$bmi = round($bmi,2);
$form_values['submitted'][11] = $bmi;
$form_values['submitted_tree'][11] = $bmi;
Altri suggerimenti
round ($your_variable, 2)
PHP round
Prenderà un numero e lo circonda a 2 unità di precisione:
$foo = 105.5555;
echo round($foo, 2); //prints 105.56
O una stringa e arrotonda di 2 unità di precisione:
$bmi = "52.44444";
echo round($bmi, 2); //prints 52.44
Round a due unità precisione e forzare una precisione minima in questo modo:
$foo = 105.5555;
echo number_format(round($foo, 2), 4, '.', ''); //prints 105.5600
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow