最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Kendo Grid numbers truncate to 2 decimal places. How to make it respect what the user entered? - Stack Overflow

matteradmin3PV0评论

In this Kendo Grid demo, if you edit the numbers under "Units in Stock" and add multiple decimals (try 2.203848), it'll truncate it to 2.20. It looks like that's the default behavior.

And I know that we can specify the decimal format with {0:n4}, for example.

But what if the number of decimals is unknown or can vary? Is there any way to make the grid use the exact number that the user enters?

In this Kendo Grid demo, if you edit the numbers under "Units in Stock" and add multiple decimals (try 2.203848), it'll truncate it to 2.20. It looks like that's the default behavior.

And I know that we can specify the decimal format with {0:n4}, for example.

But what if the number of decimals is unknown or can vary? Is there any way to make the grid use the exact number that the user enters?

Share Improve this question asked Jul 8, 2015 at 15:36 dmathisendmathisen 2,3425 gold badges37 silver badges65 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6 +50

To make this work in a grid, you need to use a custom editor. Set the number of decimals to a high enough number and make sure your field format has enough places. This great answer here tweaked a bit solves your problem.

function numberEditor(container, options) {
$('<input name="' + options.field + '"/>')
        .appendTo(container)
        .kendoNumericTextBox({
            decimals: 8,
            step    : 0.01
        });
}

$("#grid").kendoGrid({
    dataSource: dataSource,
    navigatable: true,
    pageable: true,
    height: 550,
    toolbar: ["create", "save", "cancel"],
    columns: [
        "ProductName",
        { field: "UnitPrice", title: "Unit Price", format: "{0:c}", width: 120 },
        { field: "UnitsInStock", title: "Units In Stock", format: "{0:0.#########}", width: 120, editor: numberEditor },
        { field: "Discontinued", width: 120 },
        { mand: "destroy", title: "&nbsp;", width: 150 }],
    editable: true
});

});

The kendo ui grid supports any format that you can use when calling kendo.format() directly. The documentation for that is available here.

The format you are looking for is

{0:$###,###,###,###,###,###,###,###.##############} 

You will need to pad that out to however many decimal places you want to support.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far