3

I am encountering numeric issues, possible related to this problem: QGIS labels rounding error

I have a field value of 0.55 (type double NULL) that I want to display in a label as a percentage. The expression for the label is concat(FIELD*100,'%'). The label for this object is shown as 55.00000000000001%.

It only happens for certain values, but always for the same ones: 14% becomes 14.00000000000002 and 28% becomes 28.00000000000004. For other layers, the same thing happens with the ROUND function used in the label expression, for the values 57%, 58% and 110%. The problem recurs whenever those same values come up, in different layers.

Does anyone have this problem, or better yet, found a solution? For now, I get around by adding rounding, or rounding at a different place in the formula.

QGIS version is 3.22.9.

3
  • 1
    So rounding works as you want? So what is the problem? It seems like to be a rounding error (if percent number are calculated based on some values and do not result exactly in what you expect). You vould try converting the numbers to integers, using to_int() function
    – Babel
    Commented Jan 9, 2023 at 10:29
  • Rounding errors occur, but the same numeric issue occurs when I just want to convert a (already rounded) number to a percentage, e.g. 0.55 to 55%. I have work-arounds, but to_int() would probably work too. Thanks for the suggestion.
    – MobiMech
    Commented Jan 9, 2023 at 10:34
  • Changing field length might be another option, see: gis.stackexchange.com/a/430360/88814
    – Babel
    Commented Jan 9, 2023 at 10:38

1 Answer 1

3

You can format the number when you change it to a string using the format_number function:

function format_number

Returns a number formatted with the locale separator for thousands. By default the current QGIS user locale is used. Also truncates the decimal places to the number of supplied places.

Syntax

format_number(number[,places=0][,language])

[ ] marks optional components

Arguments

number number to be formatted places integer representing the number of decimal places to truncate the string to. language language (lowercase, two- or three-letter, ISO 639 language code) used to format the number into a string. By default the current QGIS user locale is used.

Examples

format_number(10000000.332,2) → '10,000,000.33' if e.g. the current locale is an English variant format_number(10000000.332,2,'fr') → '10 000 000,33'

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.