How to limit a calculated field with “real” to 1 number after the decimal?

  • Asked
  • Active
  • Viewed 5092 times
0

Hi,

I have way too many numbers after the decimal of a calculated field. How can I limit the calculated field to 1 or 2 numbers after the decimal?

Thanks for sharing your knowledge!

Florence

0

Accepted answer

Hi Florence,
What you can do is to format the number that comes from your expression by wrapping it into the format-number function. Examples:
- limit a number to 1 decimal digit: format-number(MY_NUMERIC_EXPRESSION, "#.0")
- limit a number to 2 decimal digit: format-number(MY_NUMERIC_EXPRESSION, "#.00")
Note that the format-number expression will truncate your decimal part... if you want to do something a bit more complex, then try to combine it with some other numeric functions, like round, ceiling, floor. All of these functions are core functions of the XPath language, you can find lots of documentation on internet (here for example).
Thanks,
Open Foris Team

0

Accepted answer

Thanks for the guidance. I tried using

round((cropland_tree_count div cropland_area), "#.00")

while there is no error message in the validation rule, when I preview, I get an error message "2017-05-05T13:33:29.548Z - error updating data: undefined". I replaced the " with ' but it gave me the same error message.

Almost there!!

Florence

0

Accepted answer

Hi Florence,
The expression you wrote is wrong because the function round accepts only one parameter. In theory looks even wrong to me because it will cut the decimal part of the number... What you can do is for example this:
format-number(round(cropland_tree_count * 100 div cropland_area) div 100, "#.00")
In this way the result should be the expected one... Let us know!
Thanks,
Open Foris Team

0

Accepted answer

Hi!

Well, I copy-pasted the expression (I don't understand it so much I have to say) but I end up with "2017-05-07T15:29:39.338Z - error updating data: undefined" error...

Sorry it is still a pending issue...

Cheers,

Florence

0

Accepted answer

Hi Florence,
Probably this expression gives an error when one of the operands is null... try to specify this in the Apply when condition:
cropland_tree_count > 0 and cropland_area > 0
Also please don't forget that the format-number function will work only if you use it in a calculated "Text" attribute... otherwise the formatting can result in a invalid number error if used for a "Numeric" attribute. If you just need to limit the decimal digits, maybe using only the round/ceiling/floor functions will be enought...! I hope it's more clear now!
Thanks,
Open Foris Team

That worked beautifully, thanks!!

Cheers

Florence

flandsberg flandsberg

Your answer

Markdown text. Shortcuts: Ctrl+B bold, Ctrl+I italic, Ctrl+K link, Ctrl+E code, Ctrl+Shift+V paste as plain text. Tab indents list items and code; press Escape, then Tab, to leave the text box.

Insert an image

Say what the image shows, for people who can't see it.

Keyboard shortcuts

Anywhere

?
Show this list
/
Search
g h
Go to the home page
g q
Go to the questions
g t
Go to the tags
g u
Go to the users
a
Ask a question

Lists of questions

j k
Next or previous question
Enter
Open the question

Question pages

j k
Next or previous post

Shortcuts never apply while you type in a text field. Members can turn them off in their preferences.