Skip to content

Instantly share code, notes, and snippets.

@sodogan
Created December 19, 2021 17:08
Show Gist options
  • Save sodogan/8bee4ac330c8e6cdc100f2ce881907d3 to your computer and use it in GitHub Desktop.
Save sodogan/8bee4ac330c8e6cdc100f2ce881907d3 to your computer and use it in GitHub Desktop.
sap.ui.define([], function() {
"use strict";
return {
/**
* Rounds the currency value to 2 digits
*
* @public
* @param {string} sValue value to be formatted
* @returns {string} formatted currency value with 2 digits
*/
decimalToFloat: function(sValue) {
var oLocale = new sap.ui.core.Locale("tr-TR");
var oFormatOptions = {
style: "short",
decimals: 0,
shortDecimals: 3
};
var oFloatFormat = sap.ui.core.format.NumberFormat.getFloatInstance(oFormatOptions, oLocale);
return oFloatFormat.format(sValue); // returns 1.23K (shortified number takes the shortDecimals parameter)
},
decimalToFloat2: function(sValue) {
var oLocale = new sap.ui.core.Locale("tr-TR");
var oFormatOptions = {
style: "short",
decimals: 2,
shortDecimals: 3
};
var oFloatFormat = sap.ui.core.format.NumberFormat.getFloatInstance(oFormatOptions, oLocale);
return oFloatFormat.format(sValue); // returns 1.23K (shortified number takes the shortDecimals parameter)
},
decimalToFloat14chr: function(sValue) {
if (parseFloat(sValue, 10) === 0) {
return "0";
}
var oLocale = new sap.ui.core.Locale("tr-TR");
var oFormatOptions = {
minIntegerDigits: 1,
maxIntegerDigits: 15,
minFractionDigits: 2,
maxFractionDigits: 2
};
var oFloatFormat = sap.ui.core.format.NumberFormat.getFloatInstance(oFormatOptions, oLocale);
return oFloatFormat.format(sValue); // returns 1.23K (shortified number takes the shortDecimals parameter)
},
decimalToPercent: function(sValue) {
var iValue;
if (sValue) {
iValue = parseFloat(sValue).toFixed(5) * 100;
}
return iValue;
},
numberDecrement: function(sValue) {
var iValue;
if (sValue) {
iValue = parseFloat(sValue).toFixed(2).replaceAll(".",",");
}
return iValue;
},
convStringToInt: function(sValue) {
if (sValue) {
sValue = parseInt(sValue, 10);
}
return sValue;
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment