New to Telerik UI for ASP.NET MVC? Start a free 30-day trial
Keeping Trailing Zeros in the NumericTextBox
Environment
Product | Progress Telerik UI for ASP.NET MVC NumericTextBox |
Progress Telerik UI for ASP.NET MVC version | Created 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 MVC NumericTextBox so that it shows four decimal places?
Solution
To achieve the desired result:
- Create a common function that will add the trailing zeros programmatically.
- Add the trailing zeros when you change the value through the Spin buttons by subscribing to the
Spin
event and call the common function. - Add the trailing zeros when the NumericTextBox's input is focused by subscribing to the
focus
event by using theHtmlAttributes()
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 MVC NumericTextBox Resources
- ASP.NET MVC NumericTextBox Product Page
- Telerik UI for ASP.NET MVC Video Onboarding Course (Free for trial users and license holders)
- Telerik UI for ASP.NET MVC Forums