tags:

views:

61

answers:

3

I have an HTML form where the user can type in any number:5, 8, 4.2, 5.8, anything in terms of numbers (integers or doubles).

However, I was wondering is it possible to have my php script round some of the numbers when entered into the html form, for example when a user enters 4.2, the php script should be able to round it to 4 or if it's 5.8, the php script should round it to 6.

Before I start manipulating the data in terms of calculations?

Furthermore, what is the best way to format the decimal numbers and echo them out to the screen before manipulating them and after, currently I have it set so that if a user enters 5.8 it will just echo 5.8; however, i was wondering if there are better methods or should I use printf or sprinf to somehow format the information and print out the results after I am done with my calculations.

, thanks.

+1  A: 

Hi,

The php round funcion (example) :

echo round(3.4);         // echoes 3
echo round(3.5);         // echoes 4

And php number format (example) :

$number = 1234.56;

$nombre_format_francais = number_format($number, 2, ',', ' ');
yoda
+1  A: 

Have a look at round.

Remember to check that the user input actually is a number (is_numeric. It's also smart to trim the input first). Then convert to a double: (double)$var, and round it.

gnud
A: 

From what it sounds like now, I would recommend doing the rounding on the client side via javascript in order to show the user, and then use round() on the PHP side. If you are just calculating, then why not do all of this via Javascript?

Robert DeBoer