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.
1 Answer 1
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'
to_int()
function