Having dug around in the code, there appear to be two bugs in the kendo core relating to the handling of percentages.
Bug 1: Parsing percentages to floats
The first bug is fairly simple: percentages are mapped to a number between 0 and 1 for percentages 0% to 100%. The bug appears to be in the method kendo.parseFloat(value, culture). When the value is something like "12%" it correctly identifies it as a percentage here:
however, it subsequently just removes the percentage symbol but doesn't scale the value down by dividing by 100:
This results in a value such as "12%" being changed to "1,200%" when using the percentage formatting such as:
Bug 2: Entering fractional percentages
The second problem I've found relates to using fractional percentage values. In my application I need to display percentages to 10dp and have therefore overridden the culture settings as such:
For reference, here's a stripped down version:
When providing the original model, a value such as 0.003425 is displayed as "0.3425000000%".
When clicking the cell to edit, the number editor simply displays "0".
If I then enter a fractional value such as "0.012345678912", the expected new value in the model should be "1.2345678912%" but is actually rounded to "1.0000000000%" and the value in the underlying model is saved as 0.01
Is there any way we can get more accurate values working in kendo or is there something else limiting us to 2 d.p. in the model?
Thanks in advance,
Daniel
Bug 1: Parsing percentages to floats
The first bug is fairly simple: percentages are mapped to a number between 0 and 1 for percentages 0% to 100%. The bug appears to be in the method kendo.parseFloat(value, culture). When the value is something like "12%" it correctly identifies it as a percentage here:
}
else
if
(value.indexOf(percentSymbol) > -1) {
number = percent;
symbol = percentSymbol;
}
value = value.replace(
"-"
,
""
)
.replace(symbol,
""
)
.replace(nonBreakingSpaceRegExp,
" "
)
.split(number[
","
].replace(nonBreakingSpaceRegExp,
" "
)).join(
""
)
.replace(number[
"."
],
"."
);
value = parseFloat(value);
if
(isNaN(value)) {
value =
null
;
}
else
if
(negative) {
value *= -1;
}
return
value;
This results in a value such as "12%" being changed to "1,200%" when using the percentage formatting such as:
kendo.toString(value,
'P'
)
Bug 2: Entering fractional percentages
The second problem I've found relates to using fractional percentage values. In my application I need to display percentages to 10dp and have therefore overridden the culture settings as such:
kendo.cultures[
"en-US"
].numberFormat.percent.decimals = 10;
$(
"#grid"
).kendoGrid({
autoBind:
false
,
dataSource: {
schema: {
model: {
id:
"Title"
,
fields: {
Title: { editable:
false
},
Percentage: { editable:
true
, type:
"number"
}
}
}
}
},
columns: [{
title:
"TITLE"
,
field:
"Title"
}, {
title:
"PERCENTAGE"
,
field:
"Percentage"
,
format:
"{0:P}"
}],
editable:
true
,
scrollable:
false
});
When clicking the cell to edit, the number editor simply displays "0".
If I then enter a fractional value such as "0.012345678912", the expected new value in the model should be "1.2345678912%" but is actually rounded to "1.0000000000%" and the value in the underlying model is saved as 0.01
Is there any way we can get more accurate values working in kendo or is there something else limiting us to 2 d.p. in the model?
Thanks in advance,
Daniel