Sushant Bajracharya
Sushant Bajracharya

Reputation: 400

How to print integer with 0 as a decimal value?

I have a number coming from database like this 7.0 or 7.7 . When I print the value the 7.7 prints as it is but 7.0 is printed as only 7. Is there are way to print 7.0 as 7.0 . I am using twig template in sf2.

This is how I am printing those values

{{ object.value }}

Upvotes: 3

Views: 396

Answers (4)

Sushant Bajracharya
Sushant Bajracharya

Reputation: 400

This may not be the right way to do it but at least I have got something working

First I checked the length, if the length were 1 or 2 I printed the value and appended .0 else I simply just printed the value.

{% if (device.ph_value|length == 1 or device.ph_value|length == 2)%}
    {{device.ph_value}}.0
{% else %}
    {{device.ph_value}}
{% endif %}

Upvotes: 1

Matteo
Matteo

Reputation: 39420

You can use the number_format Twig filter, as example:

{{ object.value|number_format }}

Full example:

{{ 9800.333|number_format(2, '.', ',') }}

More info in the doc here

Upvotes: 4

Andreas
Andreas

Reputation: 23968

echo number_format(object.value,1);

This should type out 7.0 as 7.0 instead of 7

Upvotes: -1

unixmiah
unixmiah

Reputation: 3143

You could do something further to preserve the value.

   $integer = filter_var(7.7, FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);

    print_r($integer);

    output
    7.7

Upvotes: -2

Related Questions