5

I have a GeoPackage table with coordinates and heights. I only want 3 decimals for the coordinates and 2 decimals for the heights.

In the past I could easily define the precision in the field calculator, but the field is greyed out for GeoPackage layers. It only works for temporary layers.

I also tried "Refactor field" tools and defined the precision, but it gave me the table you are seeing in the screenshot.

If it was string I could use a the substr() function. But these are numeric fields. And copying to a new string field, using substr() function and copying back to numeric is too much manual work, as I run into this task frequently.

enter image description here

1
  • 3
    Because as soon as you save it as a permanent geopackage layer, you are back to the situation of having many decimals... round() is doing the trick perfectly. Commented Feb 18, 2021 at 13:46

2 Answers 2

11

When creating a field containing a decimal number, use round(yourexpression,x), with x being the number of decimals you want to have.

0
5

If no rounding effect should be applied then you simply create a new field and filling it with values from original field with doubles, see image below.

result

Mind that in this case precision should be adjusted appropriately.


Another option without rounding effect can be achieved using several String Functions: left(), strpos(), and substr()

left("float", strpos("float", '.') - 1)
|| '.'|| 
substr("float", strpos("float", '.') + 1, 3)

And get the output like

result2

If the length of values before comma always posses the same length, then you may simply use left() accordingly, e.g left("float",4).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.