New to Telerik UI for ASP.NET CoreStart a free 30-day trial

Keeping Trailing Zeros in the NumericTextBox

Environment

ProductProgress Telerik UI for ASP.NET Core NumericTextBox
Progress Telerik UI for ASP.NET Core versionCreated with the 2023.1.314 version

Description

When I enter the 0.0010 value, it renders 0.001. When the component loses focus, it re-formats the number to 0.0010.

How can I format the number in a Telerik UI for ASP.NET Core NumericTextBox so that it shows four decimal places?

Solution

To achieve the desired result:

  1. Create a common function that will add the trailing zeros programmatically.
  2. Add the trailing zeros when you change the value through the Spin buttons by subscribing to the Spin event and call the common function.
  3. Add the trailing zeros when the NumericTextBox's input is focused by subscribing to the focus event by using the HtmlAttributes() configuration method and call the common function.

You can further elaborate on the example to reflect the globalization practices and check if the decimals are properly rendered when used on multiple NumericTextBoxes.

Index.cshtml
    @(Html.Kendo().NumericTextBox<double>()
        .Name("currency")
        .Format("n4")
        .Decimals(4)
        .Value(0.0010)
        .Events(events => events.Spin("onSpin"))
        .HtmlAttributes(new {onfocus = "onFocus(event)" })
    )

For the complete implementation of the suggested approach, refer to the Telerik REPL example on keeping the trailing zeros in the NumericTextBox.

More ASP.NET Core NumericTextBox Resources

See Also