Jump to content

Float filed approximation


buothz
 Share

Recommended Posts

General question about processwire, when I create a float field and insert data system truncates the decimal point, and this is not obvious if I type the numbers 2.090000 ...

But if I want to:

- 2 exact decimal (eg 2.40 / 69.70 -> now instead of 2.4 / 69.7)

- And always keep at least 2 decimal places (eg 2,00 / 69,00 -> instead I out 2/69)

where can I do? I can choose how many decimal places to round off the menu (in fact, I put 2), but I would like to, just for the field, keep the zeros and rounding to 2 decimal places ...

Thanks in advance.

Link to comment
Share on other sites

when I create a float field and insert data system truncates the decimal point, and this is not obvious if I type the numbers 2.090000

The default precision value is 0. I can update this to assume no precision value until it is specifically set ... I think this makes sense--thanks.

2 exact decimal (eg 2.40 / 69.70 -> now instead of 2.4 / 69.7)

And always keep at least 2 decimal places (eg 2,00 / 69,00 -> instead I out 2/69)

The precision field is for rounding, not formatting. I think what you want is number_format(). You could do this in your output:

$value = number_format($page->float_value, 2); 

I could add this formatting in as a configuration option with FieldtypeFloat too, but once formatted, the value is a string, not a float. And I'm not sure it's wise for us to have this Fieldtype returning strings rather than floats.

There are also localization considerations with number formatting (decimal type and thousands separator), which are also the job of number_format.

  • Like 1
Link to comment
Share on other sites

The default precision value is 0. I can update this to assume no precision value until it is specifically set ... I think this makes sense--thanks.

The precision field is for rounding, not formatting. I think what you want is number_format(). You could do this in your output:

$value = number_format($page->float_value, 2); 

I could add this formatting in as a configuration option with FieldtypeFloat too, but once formatted, the value is a string, not a float. And I'm not sure it's wise for us to have this Fieldtype returning strings rather than floats.

There are also localization considerations with number formatting (decimal type and thousands separator), which are also the job of number_format.

perfect, resolved with

$value = number_format($page->float_value, 2); 

:)

u're a genius! :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...