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 compareto(arg bigdecimal) number compare this bigdecimal with another bigdecimal add(arg bigdecimal) bigdecimal add a bigdecimal to this bigdecimal remainder(divisor bigdecimal) bigdecimal return the remainder of dividing this bigdecimal by the specified bigdecimal multiply(arg bigdecimal) bigdecimal multiply this bigdecimal by the specified bigdecimal round() bigdecimal round this bigdecimal to the nearest whole number divide(arg bigdecimal) bigdecimal divide this bigdecimal by the specified bigdecimal subtract(arg bigdecimal) bigdecimal subtract the specified bigdecimal from this bigdecimal striptrailingzeros() bigdecimal remove any trailing zeros from the decimal part of this bigdecimal plus()d bigdecimal returns a bigdecimal whose value is (+this), and whose scale is this scale() format(arg bigdecimal, currency 3 letter international (iso 4217) currency code, locale code) string format this bigdecimal to a specific locale such as money format(somerec c rate(), “eur”, “de de”) format() string format this bigdecimal using the default pattern withlang(locale string) 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)