Decimals in number field get removed

Im using the number field to set prices and need the out put to be like 7.00 or 9.50 but the zeros and the decimal are be stripped off so it’s just display 7 or 9.5 in the front end. How can get it out putted as desired?

I can see this issue logged, and it appearts it was resovled, but even with the step option, it’s still stripping the zeros as above.

Are the zeros stripped from your content or just when outputting on the frontend?

I have “7.00” displaying in the panel field, but in the content file its just “7”

If i enter “7.35” then it gets stored correctly in content file.

I have gotten around it for now with…

  $number = $sarnie->price()->value();
  echo number_format((float)$number, 2, '.', '');

But it would be nice if i didnt have to. :slight_smile:

Have you tried toFloat()?

 $number = $sarnie->price()->toFloat();

I did yes, that still strips the zeros (or doesnt add them).

Ah, sorry, I missed that the zeros get stripped from the content file, so using toFloat() won’t add them again.

If I remember correctly, there was an issue once, maybe a won't fix because the zeros can be added using code. Have to check.

Edit: https://github.com/getkirby/kirby/issues/1748#issuecomment-541022656

I did see a couple of issues around it in github. For now, i think ill just turn the above solution into a custom field method, since i need this all over the place.

Sounds sensible :upside_down_face:

Any news here? I still have the problem with stripped off zeros…

As far as I know, this has been tagged as “won’t fix”, so please use number_format() to format your numbers as required.

So, I have been having issues with this still… Zeros in decimals are getting removed. Not sure I understand Pixelijn’s “use number_format()” solution…

What is working for me is just changing the price field from a Number to a text field. Seems to be working with my shopping cart just fine.

Its in my post above…

$number = $page->yourfield()->value();
  echo number_format((float)$number, 2, '.', '');

If you have for example “7” stored in the content file from a number field, the above code will display it as “7.00” on the front end.

The downside of a text input instead of the number field is that the field doesn’t automatically validate a number input.

Oh, yes… I had already tried that, but for example, was getting $5.255.25 instead of 5.25 which is was was saved in my content txt file.

That looks as if you were echoing the number twice!

This is what i was rendering on the page per @jimbobrjames

<?= $number = $page->price()->value(); echo number_format((float)$number, 2, '.', ''); ?>

That’s two echo statements, hence the result, should be

<?php 
$number = $page->price()->value(); 
echo number_format((float)$number, 2, '.', ''); 
?>

<?= is the short echo statement

Thats not what i posted…you need it like

<?php  $number = $page->price()->value(); echo number_format((float)$number, 2, '.', ''); ?>

<?= is short for echo

lol post clash :slight_smile:

1 Like