6

I have a shapefile with attribute table that has number fields that have five zeros after the decimal point, having been imported that way. I don't need decimals and want the figures as simple intigers. How can I convert them in QGIS?

3
  • 3
    Welcome to GIS:SE @David Heslop! If you create a new integer field, you can use the name of the field with numbers as the expression (e.g. "fieldName"). Or you can use the expression to_int( "fieldName" ) to convert your real number to integer.
    – Joseph
    Dec 9, 2016 at 12:12
  • not true: you cannot use to_int("yourfieldname") with a real number, only with a string... please check before answering... :-)
    – ddb
    Apr 28, 2019 at 12:09
  • Well, it's true what's documented is the use of to_int to convert from string to integer, but it's also true that it works to directly convert from double numbers to integer, at least in QGIS 3.16. You can try in Field Calculator, to_int(3.4) for example gives 3 and to_int(3.6) 4. That is, it rounds the real number.
    – Asier
    Nov 28, 2020 at 14:12

2 Answers 2

5

Open attribute table and go to field calculator, then create new column (integer) with expression "YourColumnName".

Obviously you have to change YourColumnName with name of your column where you have old values.

1
  • 1
    That is absolutely brilliant Works fine. - thank you. I even know how to spell "integer" now!!! Dec 9, 2016 at 15:32
1

To convert real to integer in an expression, you can round the real number, convert to string and then convert to integer:

to_int( to_string( round("yourfieldname") ) )

It is a bit convoluted, but it works for me.

1
  • You don't need to convert it to string or round it. to_int(doublenumber) rounds it to an integer in field calculator.
    – Asier
    Nov 28, 2020 at 14:13

Your Answer

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

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