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

asked 04 May '17, 20:01

flandsberg's gravatar image

flandsberg
10122979
accept rate: 0%


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

permanent link

answered 05 May '17, 05:05

Stefano%20%28OF%29's gravatar image

Stefano (OF) ♦♦
4.8k119
accept rate: 19%

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

permanent link

answered 05 May '17, 15:34

flandsberg's gravatar image

flandsberg
10122979
accept rate: 0%

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

permanent link

answered 06 May '17, 19:07

Stefano%20%28OF%29's gravatar image

Stefano (OF) ♦♦
4.8k119
accept rate: 19%

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

permanent link

answered 07 May '17, 17:30

flandsberg's gravatar image

flandsberg
10122979
accept rate: 0%

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

permanent link

answered 07 May '17, 20:32

Stefano%20%28OF%29's gravatar image

Stefano (OF) ♦♦
4.8k119
accept rate: 19%

That worked beautifully, thanks!!

Cheers

Florence

(08 May '17, 19:15) flandsberg flandsberg's gravatar image
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×494

question asked: 04 May '17, 20:01

question was seen: 3,839 times

last updated: 08 May '17, 19:15