I want to round a number to 2 decimal places in SAPUI5. Could anyone help?

The RoundingMode function is used for rounding numbers in SAPUI5 and it uses these parameters: the number to be rounded and how many decimal digits to display. This is particularly useful when working with currency values or percentages that need to be displayed with consistent decimal places.

Using Float Type with Format Options

You can round a number to 2 decimal places by using the sap.ui.model.type.Float type with specific format options in your data binding ?

<Input value="{
    path: 'helloPanel>/recipient/amount',
    type: 'sap.ui.model.type.Float',
    formatOptions: {
        maxFractionDigits: 2,
        roundingMode: 'away_from_zero'
    }
}"/>

Format Options Explained

The format options control how the number is displayed ?

  • maxFractionDigits: 2 ? Limits the number to maximum 2 decimal places
  • roundingMode: 'away_from_zero' ? Specifies the rounding behavior when the number has more than 2 decimal places

Alternative Rounding Modes

You can use different rounding modes based on your requirements ?

// Other available rounding modes
roundingMode: 'towards_zero'    // Rounds towards zero
roundingMode: 'up'              // Always rounds up
roundingMode: 'down'            // Always rounds down
roundingMode: 'half_towards_zero' // Standard rounding
roundingMode: 'half_away_from_zero' // Standard rounding

Conclusion

Using the Float type with appropriate format options provides a clean and consistent way to display numbers with exactly 2 decimal places in SAPUI5 applications, ensuring proper formatting for financial and numerical data.

Updated on: 2026-03-13T18:22:57+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements