I climbed on an Internet - I did not find anything, it only climbs my head from the last character and deletes if it is zero, but surely there is a more beautiful way?
- For point and comma preg_replace ('/ (\ d + [.,] \ D +?) 0 * /', '$ 1', '0.100'); - alexlz
6 answers
rtrim('0.100', '0') - Works only with a string representing a real number. With an integer number, for example
100, it will return 1. - userlond
If you need to remove the extra zeros on the output, then use the printf () and sprintf () functions. For example:
$money = 12.12345; $format = sprintf("%01.2f", $money); echo $format; Displays only 12.12.
- 2It is also worth adding that the number will round up to a given accuracy.
sprintf("%.2f", 12.12945);will bring: 12.13 - Alex Kapustin
well, yes, in this case rtrim ('0.100', '0') is best; for clearly implied output somewhere.
- Great, guys. Thank you very much :) The only thing is that if the database is an integer, then, for example, "2." is output, if necessary, we cut the point off like this: rtrim (rtrim ($ res-> fields ['z_phaze'], '0'), '. '); - Nikoole
- oneIt can be a single function: rtrim ('2.00', '0.'); // 2 - Ilya Pirogov
- @Ilya Pirogov, works only with a string representing a real number in the interval (-10; 10). With a number, for example,
100.00, it will give1. - userlond
Em. Something else, my, you are thinking up great bikes, there is a ready-made solution round ()
$val=0.100; $new_value=round($val,1); echo $new_value; // выведет 0,1 Why invent
it only climbs into the head from the last character and deletes if it is zero
It's just gut.
- one: D It does not vevedet round without a 0.1;) for that matter. - Alex Kapustin
- Well, output the output \ + round. but the cycle to run on the number of IMHO nonsense. - Artem
- @Shrek And in
round($val,1)where did the number 1 come from? - alexlz - @alexlz> 0,100 = 0.1 in the question is up to 1 character after the dot. So clearer? In general, I wrote it myself. And who came up with the figure 1, I don’t know, it’s not even interesting, but I use it :) - Artem
- one@Shrek Yes, I’m talking about the fact that now we have to wait for the question of how to remove the extra zeros in the tail of "0.120". - alexlz
first, the round work will produce the identical sprintf result.
secondly there is no problem:
$val=0.100; echo $val; will display: 0.1
On the other hand, if we specify a number as a string:
$val="0.100"; echo $val; will display: 0.100
Therefore, we will convert the string to a number and we will get what we need:
$val="0.100"; echo (float)$val; will leave: 0.1
- Why set the number line? - Artem
- oneWell, I can only explain this problem with this. that the number is specified by a string / is cut out from somewhere / ... / but the result is a string. Otherwise there wouldn’t be a question - Alex Kapustin
(float) with very small numbers will give E notation. Type: 4.343434E-5