This is a migrated thread and some comments may be shown as answers.

2 Bugs: Fractional percentage handling

2 Answers 274 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Daniel
Top achievements
Rank 1
Daniel asked on 23 Apr 2012, 11:14 AM
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:
} else if (value.indexOf(percentSymbol) > -1) {
    number = percent;
    symbol = percentSymbol;
}
however, it subsequently just removes the percentage symbol but doesn't scale the value down by dividing by 100:
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;
For reference, here's a stripped down version:
$("#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 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

2 Answers, 1 is accepted

Sort by
0
Daniel
Top achievements
Rank 1
answered on 23 Apr 2012, 11:58 AM
To answer my own second question. The issue with the rounding on entering a value is that when writing the value to the model, it rounds based on the current culture's numberFormat.decimals property.

To allow 10 d.p. as a percentage I added the line:
kendo.cultures["en-US"].numberFormat.decimals = 12;
after the line:
kendo.cultures["en-US"].numberFormat.percent.decimals = 10;

Personally I think this should be automatically inferred, but this is a decent workaround.
0
Georgi Krustev
Telerik team
answered on 02 May 2012, 10:45 AM
Hello Daniel,

Straight upto your questions:

#1:
This is a known issue. Currently the kendo.parseFloat does not parse the percentage value correctly. We will address the problem for the upcoming service pack of the Kendo scheduled for the middle of May.

#2:
Thank you for reporting this issue. It seams that the numerictextbox expects a numeric format which is in a lower case text. As a workaround for now you can set format like this:

format: "{0:p}"

Hence setting the percent decimals should be enough:
kendo.cultures["en-US"].numberFormat.percent.decimals = 10;

As a gratitude for your involvement I have updated your Telerik points.

Regards,
Georgi Krustev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
Grid
Asked by
Daniel
Top achievements
Rank 1
Answers by
Daniel
Top achievements
Rank 1
Georgi Krustev
Telerik team
Share this question
or