Answers to: How to limit a calculated field with “real” to 1 number after the decimal?http://www.openforis.org/support/questions/1244/how-to-limit-a-calculated-field-with-real-to-1-number-after-the-decimal<p>Hi,</p>
<p>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? </p>
<p>Thanks for sharing your knowledge!</p>
<p>Florence</p>enSun, 07 May 2017 20:32:25 +0200Answer by OF Collecthttp://www.openforis.org/support/questions/1244/how-to-limit-a-calculated-field-with-real-to-1-number-after-the-decimal/1257<p>Hi Florence,<br>
Probably this expression gives an error when one of the operands is null... try to specify this in the <strong>Apply when</strong> condition:<br>
<strong>cropland_tree_count > 0 and cropland_area > 0</strong><br>
Also please don't forget that the <strong>format-number</strong> 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!<br>
Thanks,<br>
Open Foris Team</p>OF CollectSun, 07 May 2017 20:32:25 +0200http://www.openforis.org/support/questions/1244/how-to-limit-a-calculated-field-with-real-to-1-number-after-the-decimal/1257Answer by flandsberghttp://www.openforis.org/support/questions/1244/how-to-limit-a-calculated-field-with-real-to-1-number-after-the-decimal/1256<p>Hi!</p>
<p>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...</p>
<p>Sorry it is still a pending issue...</p>
<p>Cheers,</p>
<p>Florence</p>flandsbergSun, 07 May 2017 17:30:26 +0200http://www.openforis.org/support/questions/1244/how-to-limit-a-calculated-field-with-real-to-1-number-after-the-decimal/1256Answer by OF Collecthttp://www.openforis.org/support/questions/1244/how-to-limit-a-calculated-field-with-real-to-1-number-after-the-decimal/1255<p>Hi Florence,<br>
The expression you wrote is wrong because the function <em>round</em> 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:<br>
<strong>format-number(round(cropland_tree_count * 100 div cropland_area) div 100, "#.00")</strong><br>
In this way the result should be the expected one... Let us know!<br>
Thanks,<br>
Open Foris Team </p>OF CollectSat, 06 May 2017 19:07:31 +0200http://www.openforis.org/support/questions/1244/how-to-limit-a-calculated-field-with-real-to-1-number-after-the-decimal/1255Answer by flandsberghttp://www.openforis.org/support/questions/1244/how-to-limit-a-calculated-field-with-real-to-1-number-after-the-decimal/1250<p>Thanks for the guidance. I tried using </p>
<p>round((cropland_tree_count div cropland_area), "#.00")</p>
<p>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.</p>
<p>Almost there!! </p>
<p>Florence</p>flandsbergFri, 05 May 2017 15:34:41 +0200http://www.openforis.org/support/questions/1244/how-to-limit-a-calculated-field-with-real-to-1-number-after-the-decimal/1250Answer by OF Collecthttp://www.openforis.org/support/questions/1244/how-to-limit-a-calculated-field-with-real-to-1-number-after-the-decimal/1247<p>Hi Florence,<br>
What you can do is to format the number that comes from your expression by wrapping it into the <em>format-number</em> function. Examples:<br>
- limit a number to 1 decimal digit: <strong>format-number(MY_NUMERIC_EXPRESSION, "#.0")</strong><br>
- limit a number to 2 decimal digit: <strong>format-number(MY_NUMERIC_EXPRESSION, "#.00")</strong><br>
Note that the <strong>format-number</strong> 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 <em>round</em>, <em>ceiling</em>, <em>floor</em>. All of these functions are core functions of the XPath language, you can find lots of documentation on internet (<a href="https://www.w3schools.com/xml/xsl_functions.asp#numeric">here</a> for example).<br>
Thanks,<br>
Open Foris Team</p>OF CollectFri, 05 May 2017 05:05:56 +0200http://www.openforis.org/support/questions/1244/how-to-limit-a-calculated-field-with-real-to-1-number-after-the-decimal/1247