Servicely Administration
Scripting
Money API (BigDecimal)
4 min
when scripting, calculating decimals can often be problematic on certain coding languages however, to remediate this, we have implemented a library within the platform, to allow you to easily and accurately calculate decimal values this is known as “bigdecimal” and for those who have a java background, may have prior exposure to how this operates this is often used for money fields within servicely, due to the fact when you get a money value from a money field, it will be a bigdecimal object creation of a bigdecimal object when creating a bigdecimal object, you can pass in a variety of “types” and then calculate them together an example of this is as per below // bigdecimal can be build // from a number bigdecimal(0 1) // from string bigdecimal("0 1") //or another bigdecimal let dot2 = bigdecimal("0 2") bigdecimal(bigdecimal(0 1) multiply(bigdecimal(3))) available api options function returns description number compare this bigdecimal with another bigdecimal bigdecimal add a bigdecimal to this bigdecimal bigdecimal return the remainder of dividing this bigdecimal by the specified bigdecimal bigdecimal multiply this bigdecimal by the specified bigdecimal bigdecimal round this bigdecimal to the nearest whole number bigdecimal divide this bigdecimal by the specified bigdecimal bigdecimal subtract the specified bigdecimal from this bigdecimal bigdecimal remove any trailing zeros from the decimal part of this bigdecimal bigdecimal returns a bigdecimal whose value is (+this), and whose scale is this scale() string format this bigdecimal to a specific locale such as money format(somerec c rate(), “eur”, “de de”) string format this bigdecimal using the default pattern bigdecimal return a copy of this bigdecimal with the specified localesuch as de de examples // localised format bigdecimal("0 1") withlang("fr fr") format() // 0,1 // strict decimal computation bigdecimal("0 1") multiply(bigdecimal(3)) // 0 30 if this was done with javascript directly, it would be (0 1 3 = 0 30000000000000004)