Any help on this would be much appreciated:

I have a custom node type with a field of the "Float" type. For some reason, the value of this field is automatically rounding to one decimal point when the node is saved. This is not good for me, as I need it to store the original floating point number with all decimal places intact.

Any ideas?

I first noticed this issue when working on a module that saves the node programatically, but I checked the standard edit node form and it, too, does the same thing (for instance, a field value of 1234.56 turns into 1234.5) once you click save and view the updated node.

If it matters to anyone, I wrote a short PHP snippet to check the value of the field in the node object just before node_save() was called, and I was able to confirm that the full floating point number is intact at that point. When I reload the node object after it has been saved, however, the value is now rounded:

<?php 
print "Original: ".$node->field_myfloat['und'][0]['value']."<BR>";
node_save($node);
$newnode=node_load($node->nid);
print "After Save: ".$newnode->field_myfloat['und'][0]['value'];
?>

Results in the following:

Original: 74173.59
After Save: 74173.6

Comments

mrjavarava’s picture

My float field values get rounded up too. Any help?

sumitgupta2004’s picture

I am facing the same issue. Did you find a solution? The decimal field type saves just fine. Do I need to convert float type to decimal type?

jyraya’s picture

Hello,

I am facing the same issue with Decimal field.
If I type 0.08, it saves 0.1.

TechNikh’s picture

I have same issue in Drupal 8.

Cheers,
TechNikh

In 30 seconds set up Automated Visual testing of your website. Zero coding. https://drupal.org/project/drulenium
Ever dreamed of styling your view, We have a solution for you. https://drupal.org/project/views_stylizer

javivf’s picture

Try to add step property

$form['test_float'] = [
  '#type' => 'number',
  '#title' => $this->t('Test'),
  '#default_value' => 0,
  '#step' => 0.0001,
];

temkin’s picture

Had the same issue in Drupal 8. Was able to overcome it by using Number (decimal) field, instead of Number (float). Give it a try!

DustinYoder’s picture

I have the same issue still and I guess it's just a floating point glitch somewhere with how computers store floats.  I tracked my issue down to the float data type in mysql. That limits my numbers to 6 digits, so they round to 6. I am assuming that is what most of you are experiencing too. Change to decimal field in drupal or just leave as float in drupal and set the column in the database directly to 'double' I had to edit the node__field_name and node_revision__field_name tables both. They have a field_name_value column that will be a float.  Let me know if this can cause issues for drupal to be using a double column in the db for this float field.

Marmouset’s picture

Good approach !

When I enter this value for a float field : 9955908.62 Drupal sent me a 9955908.6 !! Really weird...
Changing it to double fixed the issue !

Thank you for saving me so much time =)

GundarTB’s picture

You can now use the Change Field Type module to change your float fields into Decimal fields and preserve the data.

Floats can cause rounding issues and this is particularly problematic if you are dealing in currency. 

Use the module to change the offending fields and then enable them on the form display. - all done.