...
Code Block |
---|
|
function amountFormatter(val) {
//formats a negative number with brackets (NN.NN)
if (val !== null) {
if(parseFloat(val) < 0){
var newVal = val.replace(/-(?=\d)/,""); //remove minus sign -
return '(' + newVal + ')';
}else{
return val;
}
}
} |
Another example shows how UI5's built-in number formatters can be used from within a formatter function. In this case, a floating point number has its sign 'reversed', ie formats a positive number as a negative number, and vice-versa.
In the example the Amount field is a floating point number named DMBTR.
Instructions:
- In the Advanced Property of the field DMBTR, set the binding as:
text="{path : 'DMBTR',type : 'sap.ui.model.type.Float',formatOptions: { groupingEnabled : true, decimals: 0}, formatter: 'changeSignFormatter'}
- In a Script node, create the function 'changeSignFormatter' as:
Code Block |
---|
|
function changeSignFormatter(val) {
//changes a number from a positive to a negative or vice-versa
if (val !== null) {
var oFormat = sap.ui.core.format.NumberFormat.getFloatInstance({
groupingEnabed: true,
decimals: 0
});
var rawVal = oFormat.parse(val); //takes the given 'string' value float number (eg '-1,234.00') and converts to float
var newVal = rawVal * -1;
return oFormat.format(newVal);
}
} |