Show TOC

sap.ui.core.format.NumberFormatLocate this document in the navigation structure

The NumberFormat class can be used to parse a string representing a number (float or integer) into a JavaScript number and vice versa (also known as format).

The following format options are possible. For unspecified parameters, default values according to the type are used:

  • minIntegerDigits: minimal number of non-fraction digits
  • maxIntegerDigits: maximal number of non-fraction digits
  • minFractionDigits: minimal number of fraction digits
  • maxFractionDigits: maximal number of fraction digits
  • groupingEnabled: enable grouping (show the grouping separators)
  • groupingSeparator: used grouping separator
  • decimalSeparator: used decimal separator

The following code snippet shows an example how the NumberFormat class can be used:

var fValue = 20001000.563; 
   
jQuery.sap.require("sap.ui.core.format.NumberFormat");
var oNumberFormat = sap.ui.core.format.NumberFormat.getFloatInstance({
  maxFractionDigits: 2,
  groupingEnabled: true,
  groupingSeparator: ",",
  decimalSeparator: "."
}); //Returns a NumberFormat instance for float
/*var oNumberFormat = sap.ui.core.format.NumberFormat.getIntegerInstance({
  maxFractionDigits: 0,
  groupingEnabled: false
}); //Returns a NumberFormat instance for integer*/

var oField = new sap.ui.commons.TextField();
oField.setValue(oNumberFormat.format(fValue)); // Set the formatted value on the text field
oField.attachChange(function(){
  //Parse the user input
  fValue = oNumberFormat.parse(oField.getValue());
    alert(fValue);
});