Number Wrapper
NumberWrapper¶
Provides utility methods for manipulating a Number. On the following methods, wrap is a NumberWrapper.
Tip
- to convert the number to a string, use the
toStringmethod or define the variable as a String
Reading¶
get¶
Retrieves the wrapped Number
Syntax
Number wrap.get()
Example
final value = p6.wrap.of 123456.78 get()
assert value == 123456.78
toString¶
Retrieves the String representation of the wrapped value using the optional number format.
Syntax
String wrap.toString()
Example
final s = p6.wrap.of 123456.78 toString()
final String s = p6.wrap.of 123456.78
Formatting¶
format¶
Formats the number using the specified format.
Syntax
NumberWrapper wrap.format(String format)
Example
final String value = p6.wrap.of 123456.78 format '#,##0.000'
assert value == "123,456.780"
round¶
Rounds the number to the specified number of decimal places.
Syntax
NumberWrapper wrap.round(int precision)
Example
final value = p6.wrap.of 123456.78 round 1 get()
assert value == 123456.8
Arithmetic¶
calc¶
Perform a mathematics operation on the number.
Note
The operator string should be one of the following:
+indicates addition-indicates substraction*indicates multiplication/indicates division
Tip
Multiplications and divisions can generate numbers with decimal. You can use the round method to round the result.
Warning
Dividing by zero throws a P6Exception.
Syntax
NumberWrapper wrap.calc(String operator, Number value)
Example
final String value = p6.wrap.of 123.45 calc('*', 67.89) round 2
assert value == "8381.02"
calc (from String)¶
Performs the same action as calc but with a string value. The string is converted to a BigDecimal before the operation.
Syntax
NumberWrapper wrap.calc(String operator, String value)
Example
final String value = p6.wrap.of 123.45 calc('*', '67.89') round 2
assert value == "8381.02"
plus / minus / multiply / div¶
New Feature
Since 6.10.24
Readable aliases for calc. Each accepts either a Number or another NumberWrapper, and enables Groovy operator overloading (+, -, *, /). times is an alias for multiply, and divide an alias for div.
Syntax
NumberWrapper wrap.plus(Number value) // + operator
NumberWrapper wrap.minus(Number value) // - operator
NumberWrapper wrap.multiply(Number value) // * operator (alias: times)
NumberWrapper wrap.div(Number value) // / operator (alias: divide)
Example
final total = p6.wrap.of(123.45).plus(67.89)
final net = p6.wrap.of(100) + 20 // operator overloading
assert net.get() == 120
Comparisons¶
gt / gte / lt / lte / eq¶
New Feature
Since 6.10.24
Compares the wrapped value against another Number. Comparisons are exact (performed on BigDecimal).
Syntax
boolean wrap.gt(Number value) // greater than
boolean wrap.gte(Number value) // greater than or equal
boolean wrap.lt(Number value) // less than
boolean wrap.lte(Number value) // less than or equal
boolean wrap.eq(Number value) // equal
Example
assert p6.wrap.of(10).gt(5)
assert p6.wrap.of(10).eq(10.0)
isBetween¶
New Feature
Since 6.10.24
Inclusive range test.
Syntax
boolean wrap.isBetween(Number min, Number max)
Example
assert p6.wrap.of(5).isBetween(1, 10)
min / max¶
New Feature
Since 6.10.24
Returns the smaller (min) or larger (max) of the wrapped value and the given Number.
Syntax
NumberWrapper wrap.min(Number value)
NumberWrapper wrap.max(Number value)
Example
final capped = p6.wrap.of(120).min(100) get()
assert capped == 100
Sign & kind¶
isPositive / isNegative / isZero¶
New Feature
Since 6.10.24
Tests the sign of the wrapped value.
Syntax
boolean wrap.isPositive()
boolean wrap.isNegative()
boolean wrap.isZero()
Example
assert p6.wrap.of(-3).isNegative()
assert p6.wrap.of(0).isZero()
isInteger¶
New Feature
Since 6.10.24
Returns true when the wrapped value has no fractional part.
Syntax
boolean wrap.isInteger()
Example
assert p6.wrap.of(10.0).isInteger()
assert !p6.wrap.of(10.5).isInteger()
Math¶
abs¶
New Feature
Since 6.10.24
Returns the absolute value.
Syntax
NumberWrapper wrap.abs()
Example
final value = p6.wrap.of(-12.3) abs() get()
assert value == 12.3
negate¶
New Feature
Since 6.10.24
Returns the value with its sign flipped.
Syntax
NumberWrapper wrap.negate()
Example
final value = p6.wrap.of(10) negate() get()
assert value == -10
ceil¶
New Feature
Since 6.10.24
Rounds the value up to the nearest integer.
Syntax
NumberWrapper wrap.ceil()
Example
final value = p6.wrap.of(10.2) ceil() get()
assert value == 11.0
floor¶
New Feature
Since 6.10.24
Rounds the value down to the nearest integer.
Syntax
NumberWrapper wrap.floor()
Example
final value = p6.wrap.of(10.8) floor() get()
assert value == 10.0
clamp¶
New Feature
Since 6.10.24
Constrains the value to the [min, max] range.
Syntax
NumberWrapper wrap.clamp(Number min, Number max)
Example
final bounded = p6.wrap.of(150) clamp(0, 100) get()
assert bounded == 100
Percentage¶
percentOf¶
New Feature
Since 6.10.24
Returns this value expressed as a percentage of total (value / total * 100).
Syntax
NumberWrapper wrap.percentOf(Number total)
Example
final share = p6.wrap.of(50).percentOf(200) get()
assert share == 25 // 50 is 25% of 200
pct¶
New Feature
Since 6.10.24
Applies rate percent to this value (value * rate / 100).
Syntax
NumberWrapper wrap.pct(Number rate)
Example
final vat = p6.wrap.of(200).pct(20) get()
assert vat == 40 // 20% of 200
Conversions¶
asString¶
New Feature
Since 6.10.24
Returns the value as a StringWrapper, honouring the active format.
Syntax
StringWrapper wrap.asString()
Example
final String label = p6.wrap.of(1234.5) format '#,##0.00' asString()
assert label == '1,234.50'
asInt / asLong / asBigDecimal¶
New Feature
Since 6.10.24
Converts the wrapped value to a primitive int/long or to a BigDecimal.
Syntax
int wrap.asInt()
long wrap.asLong()
BigDecimal wrap.asBigDecimal()
Example
assert p6.wrap.of(123.45).asInt() == 123
assert p6.wrap.of(123.45).asBigDecimal() == 123.45
asFileSize¶
New Feature
Since 6.10.24
Formats a byte count as a human-readable file size using binary units (1024). Pairs with
FileWrapper.size().
Syntax
StringWrapper wrap.asFileSize()
Example
assert p6.wrap.of(1536).asFileSize() == '1.5 KB'
final label = p6.wrap.of('/opt/data.zip').asFile().size().asFileSize()
Combined example¶
Parse an amount, add VAT, round, and format — then use the result in comparisons.
final net = p6.wrap.of('1234.5').asNumber()
final gross = net.plus(net.pct(20)).round(2) // net + 20% VAT
assert gross.format('#,##0.00 €') == '1,481.40 €'
assert gross.isBetween(1000, 2000)
assert gross.gt(net.get())