Difference between number_format vs round in php

avatar
Mar 29, 2023 · Snippet · 1 min, 100 words

Both of that functions work the same and make a . which one you want like I have a number it's 10. So I want this like that 10.000 in PHP. So number_format and round both work fine. But the problem is number_format makes a ( , ) in 4 digits where round doesn't do that. example, 

number_format("1000", "2") // 1,000.00
round("1000", "2") // 1000.00

we can get an error if we use abs in number_format. example,

abs(number_format("-100", "2")) // 100.0
abs(number_format("-1000", "2")) // get and error

Comments

No comments yet…