Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

number_format() in php vs toFixed() in jQuery for validation

I am having some data calculation on my client side, which I want to validate on the server end. Let's say my calculated value result is 34.55

(34.55).toFixed(1) gives me 34.5

Strangely, (34.555).toFixed(1) gives me 34.6

number_format(34.55, 1, '.', '') gives me 34.6

Can anyone suggest me an exact equivalent of either of the functions?

like image 801
Annapurna Avatar asked Feb 15 '26 02:02

Annapurna


1 Answers

round(1.95583, 2);  // 1.96

round( 1.55, 1, PHP_ROUND_HALF_DOWN); //  1.5

round( 1.54, 1, PHP_ROUND_HALF_DOWN); //  1.5

round( 1.55, 1, PHP_ROUND_HALF_EVEN); //  1.6

round( 1.54, 1, PHP_ROUND_HALF_EVEN); //  1.5

if you have any questions, ask

like image 178
Иван Ясинский Avatar answered Feb 16 '26 17:02

Иван Ясинский



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!