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.
opened 04:05PM - 14 Nov 19 UTC
closed 12:47PM - 19 Nov 19 UTC
**Describe the bug**
If you enter a decimal value into a number field that ha… s no decimals defined, as soon as the field is no longer focused, the decimals are cut off without an further hint that there was something wrong with the data. Shouldn't the field validate the entered value?
**To Reproduce**
Steps to reproduce the behavior:
1. Define a number field without the step option
2. Go to the Panel and enter a number with decimals and save.
3. See as when you leave the field, the decimals are removed without any explanation, but the save bar pops up again.
**Expected behavior**
Maybe the field should come up with a warning rather than popping up the save bar and auto-removing the decimals as the current behavior is somewhat confusing.
**Kirby Version**
Tested with Kirby 3.3.0.
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.
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.
obear
May 19, 2021, 2:56pm
10
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.
texnixe
December 3, 2021, 5:41pm
14
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.
texnixe
December 3, 2021, 7:06pm
16
Emmit_Jones:
$5.255.25
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, '.', ''); ?>
texnixe
December 3, 2021, 7:56pm
18
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