monster.util.formatPrice()
Note
Although monster.util#formatPrice
is not strictly deprecated (as in “removed from the util module”), it is now considered a legacy function and should be avoided as it might be removed in the future. For similar formatting functionalities, use monster.util#formatNumber
instead.
Syntax
monster.util.formatPrice(args);
Parameters
args
is a mandatory Object
parameter with the following properties:
Key | Description | Type | Default | Required |
---|---|---|---|---|
price | Price to format (number or string representation of a number). | Number , String | true | |
digits | Number of digits to appear after the decimal point (if not specified, integers will have no digits and floating numbers with at least one significant number after the decimal point will have two digits). | Number | 2 | false |
withCurrency | Whether or not to show the currency symbol. | Boolean | true | false |
Return value
A String
representation of price
showing the specified number of digits as well as the currency symbol.
Errors
"price" is not a valid number or not castable into a number
:price
isNaN
, of a type other thanNumber
and not castable into a validNumber
"digits" is not a positive integer
:digits
is defined but not a positiveNumber
integer"withCurrency" is not a boolean
:withCurrency
is defined but not of typeBoolean
Description
The monster.util.formatPrice()
method is used to easily format prices.
Examples
Format prices with currency
monster.util.formatPrice({
price: 3
});
// output: "$3"
monster.util.formatPrice({
price: '3.3'
});
// output: "$3.30"
monster.util.formatPrice({
price: 3.3333
});
// output: "$3.33"
monster.util.formatPrice({
price: 5,
digits: 3
});
// output: "$5.000"
monster.util.formatPrice({
price: '5.5',
digits: 3
});
// output: "$5.500"
monster.util.formatPrice({
price: -5.5555,
digits: 3
});
// output: "-$5.556"
Format prices without currency
monster.util.formatPrice({
price: 5.001,
withCurrency: false
});
// output: "5.00"