2

How do we we add a zero in front of the decimal place in PHP?

I want to convert .96 to 0.96

Now in my php code, I'm fetching data by using wordpress get_attribute

<td><?php echo $_product->get_attribute('pa_carat');?></td>
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
  • 1
    Possible duplicate of [Formatting a number with leading zeros in PHP](https://stackoverflow.com/questions/1699958/formatting-a-number-with-leading-zeros-in-php) – jordiburgos Aug 06 '17 at 10:52
  • OP look at the link above. Choosing number format will give you a headache in the long run. – Andreas Aug 06 '17 at 11:27
  • Here is sprintf also included. Only floatval produce the correct value each time. https://3v4l.org/opkMj – Andreas Aug 06 '17 at 11:53

2 Answers2

3

The function you want is number_format

echo number_format(0.96, 2);

Alternatively you can just check for and prepend it if it is missing.

if ($num[0] == '.')
  $num = '0' . $num;

This wont handle negative numbers though, you would need to write a little more logic for that.

Geoffrey
  • 10,843
  • 3
  • 33
  • 46
  • what! that was simple.. i was reading number_format in the forum and though it only adds zero behind the decimal. thanks Geoffrey – Nigel Ong Nigel ong Aug 06 '17 at 10:20
  • The answer is correct, I do prefer @Andreas's answer to my own, but it doesn't make this method any less accurate. Also the prepend option is about 10x faster then casting to float, it depends on how you want to go about it. So why the down votes? – Geoffrey Aug 06 '17 at 19:16
  • @Geoffrey `Also the prepend option is about 10x faster then casting to float` how did you come to that conclusion? As far as I can see they are pretty a close, but with the added benefit of floatval actually producing the correct values every time. Number format: https://3v4l.org/cFXoQ/perf#output vs floatval: https://3v4l.org/A9LKK/perf#output . Even if floatval is 10x slower then it still gives correct outputs, which number format does not. How can you compare speed vs incorrect output? – Andreas Aug 06 '17 at 21:32
  • @Andreas: If you know your number will always be positive, where being negative will be an error, it is safe to perform the faster operation. You compared `number_format` with `floatval` in your tests. `number_format` and `floatval` convert the string to float, then convert back from float to string, which is a slow operation. Just checking the string for a "." and prepending a "0" performs no type conversions at all. – Geoffrey Aug 06 '17 at 22:27
  • @Geoffery I thought number format was your answer. The prepend part isn't even complete code. It only handles one situation. An answer should handle all situations in my opinion (at least within normal reach). To only include code for positive numbers starting with `.` is pretty weak. Negative and integers should also work in my opinion. Once you got all that code working we can simulate a bunch of numbers and see which is fastest. Saying that your non complete answer is 10x faster isn't really true. – Andreas Aug 07 '17 at 05:13
0

Floatval()
http://php.net/manual/en/function.floatval.php
Returns the float value of a string. No need for number format or ifs.
Floatval will also handle negative numbers such as "-.96".

$s = ".96";
Echo floatval($s);

https://3v4l.org/Qt8A3

Andreas
  • 23,610
  • 6
  • 30
  • 62