You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: knowledge-base/numerictextbox-percentage-range.md
+55-78Lines changed: 55 additions & 78 deletions
Original file line number
Diff line number
Diff line change
@@ -11,6 +11,7 @@ res_type: kb
11
11
---
12
12
13
13
## Environment
14
+
14
15
<table>
15
16
<tbody>
16
17
<tr>
@@ -23,122 +24,98 @@ res_type: kb
23
24
24
25
## Description
25
26
26
-
When I use percentage and input 1 to the numeric text box it shows 100%, when i input 0.10 it shows 10%
27
+
When I use percentage and input 1 in the numeric textbox, it shows 100%, and when I input 0.10 it shows 10%.
27
28
28
-
I want that inputting 1 will show 1% and inputting 10 will show 10%
29
+
I want input of `1` to show `1%` and inputting `10` to show `10%`.
29
30
30
31
## Possible Cause
31
32
32
-
The `Format` of the numeric textbox is just a string format over the actual value. In .NET, the percentage format has a range of [0%, 100%] for values between [0, 1]. Therefore, the value of 1 is 100%, the value of 0.1 is 10%.
33
+
The `Format` of the numeric textbox is just a string format over the actual value. In .NET, the percentage format has a range of [0%, 100%] for values between [0, 1]. Therefore, the value of `1` is `100%`, the value of `0.1` is `10%`.
33
34
34
-
Determining whether the actual intent of the user is to input 20 as 20% or 20 as 2000% is up to the application - this is a heuristic task from the perspective of the Numeric Textbox component and it cannot make that decision. Thus, it keeps the user input as is, and when it loses focus it applies the designated Format.
35
+
Determining whether the actual intent of the user is to input `20` as `20%` or `20` as `2000%` is up to the application. This is a heuristic task from the perspective of the NumericTextbox component and it cannot make that decision. Thus, it keeps the user input as is, and when it loses focus it applies the designated Format.
35
36
36
37
>caption Simple way to see what values correspond to what percentage format
37
38
38
39
````CSHTML
39
-
Actual value: @thePercentage, formatted value @thePercentage.ToString("P2")
You can use the [component events]({%slug components/numerictextbox/events%}) to change the value. For example, if your application knows the range of values it expects to be always between 0-100%, divide values larger than 1 by 1000.
53
-
54
-
If you want precision to the decimal places of the percentage values, this means that you need to also set `Decimals="4"` to the numeric textbox because the first two decimal places correspond to the two-digit percentages.
55
-
56
-
>caption Change the Value in the app code to make 10% come from input "10"
54
+
Set the [NumericTextBox `Format` parameter]({%slug components/numerictextbox/overview%}#numeric-textbox-parameters) to a [custom format string with a '%' literal](https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-numeric-format-strings#character-literals):
57
55
58
-
<divclass="skip-repl"></div>
59
-
````OnChange
60
-
Actual value: @thePercentage, formatted value @thePercentage.ToString("P2")
You can achieve similar behavior with a [MaskedTextbox component]({%slug maskedtextbox-overview%}). Prepare a proper mask and parse the string to a double for later logic. The example below also shows how to use a culture-aware decimal separator:
88
73
89
-
@code{
90
-
double thePercentage { get; set; } = 12;
74
+
>caption Using the MaskedTextBox component for percent values
91
75
92
-
void ChangePercentage(double currValue)
93
-
{
94
-
// implement the desired logic here - it depends on the values you expect
95
-
// for example, this app expects percentages between 0% and 100%, so it divides by 100
96
-
// note that this is a heuristic task because 1000% may be a valid value
97
-
// and it is usually up to the user to determine what they need and want
98
-
// note 2: ValueChanged fires on every keystroke and is more invasive to the UX
You can achieve similar behavior with a Masked Textbox - prepare a proper mask (the example below shows how to also use a culture-aware decimal separator) and parse the string to a double for later logic:
0 commit comments