Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have come up with the following expression to convert 5.1234 to 5° 7`24.24"

toint ($x) || '° ' || toint((($x) - toint ($x)) * 60) ||'`' || substr( (tostring(((($x) - toint ($x)) * 60) - toint((($x) - toint ($x)) * 60)) * 60),1,5) || '"'

Here’s the problem : for some points such as 5.1234, it works. but for others, it doesn’t. I suspect the problem is the integer conversion which round up the decimal figures instead of truncating it.

Is there any other option? thanks.

share|improve this question
I can suggest a workaround. Not optimal, but if you are stuck right now, you can get your work done. Right click and Save As a CSV. Open the CSV in Excel or Google Docs, enter your formula (which should be cleaner and easier). Delete all fields except a unique identifier and converted DMS strings. Save it. Do a table join with existing layer and you are set. –  spatialthoughts Jul 2 '12 at 10:46
Be careful with negative inputs. –  whuber Jul 2 '12 at 11:37
Thanks for the tips guys. –  Obsidianz Jul 2 '12 at 22:22
Just to add to spatialthought's answer: rounding down to the nearest degree will work in Excel using the FLOOR() function. –  Micha Jul 3 '12 at 11:23

2 Answers 2

I like the expression you've put together - probably no solution in QGIS 1.8, but in QGIS 1.9-dev there's a floor() rounding function which rounds down:

floor ($x) || '° ' || floor((($x) - floor ($x)) * 60) ||'\'' || substr( (tostring(((($x) - floor ($x)) * 60) - floor((($x) - floor ($x)) * 60)) * 60),1,5) || '"'

Note the escaped apostrophe (\').

share|improve this answer

The modulo operator can be used to do truncation, but the resulting expression would be very ugly. It is prettier to use string substitution, but unfortunately QGIS doesn't expose any strpos or similar functions. Use regexp_replace($x, '\\..*', '') to get the whole part and regexp_replace($x, '^[0-9]*\\.', '') to get the decimal part. Use toreal instead of toint for calculations with the second expression to make sure there will be no rounding.

share|improve this answer
Didn't have a strpos function but does now ;) github.com/qgis/Quantum-GIS/commit/… –  Nathan W Jul 2 '12 at 10:04
tsk tsk tsk, so much whitespace noise. –  lynxlynxlynx Jul 2 '12 at 10:22
Yeah ignore that first bit it was just a bit of clean up unrelated to the new function. –  Nathan W Jul 2 '12 at 10:25
Ugly it is... will try to work it out. –  Obsidianz Jul 2 '12 at 22:39

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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