Skip to content

Commit f9b3a53

Browse files
mainmind83dimodi
andauthored
Update numerictextbox-percentage-range.md (#2379)
* Update numerictextbox-percentage-range.md Alternative solution * Update numerictextbox-percentage-range.md --------- Co-authored-by: Dimo Dimov <961014+dimodi@users.noreply.github.com>
1 parent a2d6392 commit f9b3a53

File tree

1 file changed

+55
-78
lines changed

1 file changed

+55
-78
lines changed

knowledge-base/numerictextbox-percentage-range.md

Lines changed: 55 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ res_type: kb
1111
---
1212

1313
## Environment
14+
1415
<table>
1516
<tbody>
1617
<tr>
@@ -23,122 +24,98 @@ res_type: kb
2324

2425
## Description
2526

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%.
2728

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%`.
2930

3031
## Possible Cause
3132

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%`.
3334

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.
3536

3637
>caption Simple way to see what values correspond to what percentage format
3738
3839
````CSHTML
39-
Actual value: @thePercentage, formatted value @thePercentage.ToString("P2")
40-
41-
<br />
40+
<p>NumericTextBox component <code>Value</code>: @NumericValue</p>
41+
<p>Formatted value: @NumericValue.ToString("P2")</p>
4242
43-
<TelerikNumericTextBox @bind-Value="@thePercentage" Format="P2" />
43+
<TelerikNumericTextBox @bind-Value="@NumericValue"
44+
Format="P2"
45+
Width="120px" />
4446
45-
@code{
46-
double thePercentage { get; set; } = 12;
47+
@code {
48+
private double NumericValue { get; set; } = 12;
4749
}
4850
````
4951

5052
## Solution
5153

52-
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):
5755

58-
<div class="skip-repl"></div>
59-
````OnChange
60-
Actual value: @thePercentage, formatted value @thePercentage.ToString("P2")
61-
62-
<br />
63-
64-
<TelerikNumericTextBox @bind-Value="@thePercentage" Format="P2" Decimals="4" OnChange="@ChangePercentage" />
56+
>caption NumericTextBox custom format and '%' literal
6557
58+
````CSHTML
59+
<TelerikNumericTextBox @bind-Value="@PctValue"
60+
Max="100" Min="0"
61+
Decimals="0"
62+
Format="# '%'"
63+
Step="1"
64+
Width="120px" />
6665
@code{
67-
double thePercentage { get; set; } = 12;
68-
69-
void ChangePercentage(object currValue)
70-
{
71-
// implement the desired logic here - it depends on the values you expect
72-
// for example, this app expects percentages between 0% and 100%, so it divides by 100
73-
// note that this is a heuristic task because 1000% may be a valid value
74-
// and it is usually up to the user to determine what they need and want
75-
if (thePercentage >= 1)
76-
{
77-
thePercentage = thePercentage / 100;
78-
}
79-
}
66+
private double PctValue { get; set; } = 12;
8067
}
8168
````
82-
````ValueChanged
83-
Actual value: @thePercentage, formatted value @thePercentage.ToString("P2")
8469

85-
<br />
70+
## Notes
8671

87-
<TelerikNumericTextBox Value="@thePercentage" Format="P2" Decimals="4" ValueChanged="@( (double v) => ChangePercentage(v) )" />
72+
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:
8873

89-
@code{
90-
double thePercentage { get; set; } = 12;
74+
>caption Using the MaskedTextBox component for percent values
9175
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
99-
if (currValue >= 1)
100-
{
101-
thePercentage = currValue / 100;
102-
}
103-
else
104-
{
105-
thePercentage = currValue;
106-
}
107-
}
108-
}
109-
````
76+
````CSHTML
77+
@using System.Globalization
11078
111-
## Notes
79+
<p><code>MaskedTextBoxValue</code>: @MaskedTextBoxValue</p>
11280
113-
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:
81+
<p><code>PctValue</code>: @PctValue</p>
11482
115-
````CSHTML
116-
as string: @TheStringValue
117-
<br />
118-
as double: @PercentageZeroToHundred
119-
<br />
120-
<TelerikMaskedTextBox Mask="@TheMask"
83+
<TelerikMaskedTextBox Mask="@PctMask"
12184
IncludeLiterals="true"
122-
@bind-Value="@TheStringValue">
85+
@bind-Value="@MaskedTextBoxValue"
86+
Width="120px">
12387
</TelerikMaskedTextBox>
12488
125-
@code{
126-
string TheMask { get; set; } = string.Format("00{0}00%", System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator);
127-
string TheStringValue { get; set; }
128-
double PercentageZeroToHundred => ParseDouble(TheStringValue);
129-
double ParseDouble(string stringVersion)
89+
@code {
90+
private string PctMask { get; set; } = string.Format("00{0}00%", CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator);
91+
92+
private string MaskedTextBoxValue { get; set; } = "12.34";
93+
94+
private double PctValue => ParseDouble(MaskedTextBoxValue);
95+
96+
private double ParseDouble(string stringValue)
13097
{
131-
if (string.IsNullOrEmpty(stringVersion))
98+
if (string.IsNullOrEmpty(stringValue))
13299
{
133100
return 0d;
134101
}
135-
double val;
136-
stringVersion = stringVersion.Replace("%", "");
137-
if(Double.TryParse(stringVersion, out val))
102+
103+
double parsedValue;
104+
105+
// This custom logic can vary, depending on the business requirements.
106+
stringValue = stringValue.Replace("%", "").Replace(" ", "");
107+
108+
if (Double.TryParse(stringValue, out parsedValue))
138109
{
139-
return val;
110+
return parsedValue;
140111
}
112+
141113
return 0d;
142114
}
143115
}
144116
````
117+
118+
## See Also
119+
120+
* [NumericTextBox overview]({%slug components/numerictextbox/overview%})
121+
* [MaskedTextbox overview]({%slug maskedtextbox-overview%})

0 commit comments

Comments
 (0)