Skip to content

Commit 7b6f6a1

Browse files
committed
[FIX] NumberFormat: make NumberFormat.RoundingMode guideline compliant
- The key and value should have the same string value in an UI5 enum. Thus the NumberFormat.RoundingMode needs to be adapted and we now support both all lower and all upper case letters. BCP: 1870335948 Fixes: #2169 Change-Id: Icf7e486d247698023230d7002496a9711c74b120
1 parent 0b34a2d commit 7b6f6a1

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

src/sap.ui.core/src/sap/ui/core/format/NumberFormat.js

+16-8
Original file line numberDiff line numberDiff line change
@@ -72,49 +72,49 @@ sap.ui.define([
7272
* @public
7373
* @type {string}
7474
*/
75-
FLOOR: "floor",
75+
FLOOR: "FLOOR",
7676
/**
7777
* Rounding mode to round towards positive infinity
7878
* @public
7979
* @type {string}
8080
*/
81-
CEILING: "ceiling",
81+
CEILING: "CEILING",
8282
/**
8383
* Rounding mode to round towards zero
8484
* @public
8585
* @type {string}
8686
*/
87-
TOWARDS_ZERO: "towards_zero",
87+
TOWARDS_ZERO: "TOWARDS_ZERO",
8888
/**
8989
* Rounding mode to round away from zero
9090
* @public
9191
* @type {string}
9292
*/
93-
AWAY_FROM_ZERO: "away_from_zero",
93+
AWAY_FROM_ZERO: "AWAY_FROM_ZERO",
9494
/**
9595
* Rounding mode to round towards the nearest neighbor unless both neighbors are equidistant, in which case round towards negative infinity.
9696
* @public
9797
* @type {string}
9898
*/
99-
HALF_FLOOR: "half_floor",
99+
HALF_FLOOR: "HALF_FLOOR",
100100
/**
101101
* Rounding mode to round towards the nearest neighbor unless both neighbors are equidistant, in which case round towards positive infinity.
102102
* @public
103103
* @type {string}
104104
*/
105-
HALF_CEILING: "half_ceiling",
105+
HALF_CEILING: "HALF_CEILING",
106106
/**
107107
* Rounding mode to round towards the nearest neighbor unless both neighbors are equidistant, in which case round towards zero.
108108
* @public
109109
* @type {string}
110110
*/
111-
HALF_TOWARDS_ZERO: "half_towards_zero",
111+
HALF_TOWARDS_ZERO: "HALF_TOWARDS_ZERO",
112112
/**
113113
* Rounding mode to round towards the nearest neighbor unless both neighbors are equidistant, in which case round away from zero.
114114
* @public
115115
* @type {string}
116116
*/
117-
HALF_AWAY_FROM_ZERO: "half_away_from_zero"
117+
HALF_AWAY_FROM_ZERO: "HALF_AWAY_FROM_ZERO"
118118
};
119119

120120
var mRoundingFunction = {};
@@ -2082,6 +2082,14 @@ sap.ui.define([
20822082
// Support custom function for rounding the number
20832083
fValue = sRoundingMode(fValue, iMaxFractionDigits);
20842084
} else {
2085+
// The NumberFormat.RoundingMode had all values in lower case before and later changed all values to upper case
2086+
// to match the key according to the UI5 guideline for defining enum. Therefore it's needed to support both
2087+
// lower and upper cases. Here checks whether the value has only lower case letters and converts it all to upper
2088+
// case if so.
2089+
if (sRoundingMode.match(/^[a-z_]+$/)) {
2090+
sRoundingMode = sRoundingMode.toUpperCase();
2091+
}
2092+
20852093
if (!iMaxFractionDigits) {
20862094
return mRoundingFunction[sRoundingMode](fValue);
20872095
}

src/sap.ui.core/test/sap/ui/core/qunit/types/NumberFormat.qunit.js

+16
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,22 @@ sap.ui.define(["sap/ui/core/format/NumberFormat", "sap/ui/core/Locale", "sap/ui/
962962
assert.equal(oFormat.format(-.1236), "-0.123", "-.1236");
963963
});
964964

965+
QUnit.test("float format with rounding mode: CEILING (via legacy all lower case letters: ceiling)", function (assert) {
966+
var oFormat = NumberFormat.getFloatInstance({
967+
maxFractionDigits: 3,
968+
roundingMode: "ceiling"
969+
});
970+
971+
assert.equal(oFormat.format(.1230), "0.123", ".123");
972+
assert.equal(oFormat.format(.1234), "0.124", ".1234");
973+
assert.equal(oFormat.format(.1235), "0.124", ".1235");
974+
assert.equal(oFormat.format(.1239), "0.124", ".1239");
975+
assert.equal(oFormat.format(2.1999), "2.2", "2.1999");
976+
assert.equal(oFormat.format(2.11), "2.11", "2.11");
977+
assert.equal(oFormat.format(-.1234), "-0.123", "-.1234");
978+
assert.equal(oFormat.format(-.1236), "-0.123", "-.1236");
979+
});
980+
965981
QUnit.test("float format with rounding mode: CEILING with decimals set to a string which contains a number", function (assert) {
966982
var oFormat = NumberFormat.getFloatInstance({
967983
decimals: "3",

0 commit comments

Comments
 (0)