Я работаю над функцией JavaScript, которая принимает два значения: точность десятичного значения и масштаб десятичного значения.
Эта функция должна вычислять максимальное значение, которое может быть сохранено в десятичном виде этого размера.
Например: десятичное число с точностью 5 и шкалой 3 будет иметь максимальное значение 99,999.
То, что у меня есть, делает работу, но это не элегантно. Кто-нибудь может придумать что-нибудь более умное?
Также, пожалуйста, простите за использование этой странной версии венгерской нотации.
function maxDecimalValue(pintPrecision, pintScale) {
/* the maximum integers for a decimal is equal to the precision - the scale.
The maximum number of decimal places is equal to the scale.
For example, a decimal(5,3) would have a max value of 99.999
*/
// There's got to be a more elegant way to do this...
var intMaxInts = (pintPrecision- pintScale);
var intMaxDecs = pintScale;
var intCount;
var strMaxValue = "";
// build the max number. Start with the integers.
if (intMaxInts == 0) strMaxValue = "0";
for (intCount = 1; intCount <= intMaxInts; intCount++) {
strMaxValue += "9";
}
// add the values in the decimal place
if (intMaxDecs > 0) {
strMaxValue += ".";
for (intCount = 1; intCount <= intMaxDecs; intCount++) {
strMaxValue += "9";
}
}
return parseFloat(strMaxValue);
}