Operator Type:
Operator Scope of Action:
Operator Purpose:
Operator First Added:
Operator Altered:
Function [other Function type actions]
Item [operators of similar scope]
Formatting [other Formatting operators]
Baseline
Number.precision(N)
This function makes it easier to format numbers. The function returns the associated Number-type attributes value rounded to N decimal places as Number-type data.. If $MyNumber is 268.3289 then:
$MyNumber.precision(2);
…sets $MyNumber to 268.33, whilst:
$SomeNumber = $MyNumber.precision(2);
…sets $OtherNumber to 268.33. The above examples are the equivalent of the older syntax using format():
format($MyNumber,2);
Literal numbers, e.g. 3.1415927, can also be worked with:
$MyNumber = 5.1415927.precision(2);
is 5.14
The above works but the following syntax may seem less ambiguous by using parentheses to delimit the literal number:
$MyNumber =(5.1415927).precision(2);
is 5.14
Since Number.precision() was added, format() has been supplemented by a Number.format(). The latter, in single input version equates to Number.precision(). However that Number.format() outputs a String-type, even if that can be coerced back to a number .
The .precision() function can be used to add trailing zeroes to a decimal. If $MyNumber is 214.40, it will display as 214.4 which can be unhelpful if this actually represents £214.40. $MyNumber.precision(2) will return 214.40 but be careful about under-the-hood number/string coercion such as can happen in contexts like $DisplayExpression.
$MyOtherNumber = $MyNumber.precision(2);
gives 214.4
$MyString = $MyNumber.precision(2);
gives "214.40"
In a $DisplayExpression:
$MyNumber.precision(2);
gives "214.40"
$Name+" : "+$MyNumber.precision(2);
gives "214.40"
$Name+ " : " + sum(children,$MyNumber).precision(2);
It may still be easier to use format() as no extra parentheses are required:
$Name+ " : " + format(sum(children,$MyNumber),2);