Skip to content

Commit 110ca4d

Browse files
kb(numericTextBox): percentage range
1 parent 87632e9 commit 110ca4d

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
---
2+
title: Percentage Format Range
3+
description: How to change the percentage display to show 10% instead of 100% when value is 1
4+
type: troubleshooting
5+
page_title: Show 10% instead of 100% when value is 1
6+
slug: numerictextbox-kb-percentage-range
7+
position:
8+
tags:
9+
ticketid: 1483526
10+
res_type: kb
11+
---
12+
13+
## Environment
14+
<table>
15+
<tbody>
16+
<tr>
17+
<td>Product</td>
18+
<td>NumericTextBox for Blazor</td>
19+
</tr>
20+
</tbody>
21+
</table>
22+
23+
24+
## Description
25+
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+
28+
I want that inputting 1 will show 1% and inputting 10 will show 10%
29+
30+
## Cause\Possible Cause(s)
31+
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+
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+
36+
>caption Simple way to see what values correspond to what percentage format
37+
38+
````CSHTML
39+
Actual value: @thePercentage, formatted value @thePercentage.ToString("P2")
40+
41+
<br />
42+
43+
<TelerikNumericTextBox @bind-Value="@thePercentage" Format="P2" />
44+
45+
@code{
46+
double thePercentage { get; set; } = 12;
47+
}
48+
````
49+
50+
## Solution
51+
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"
57+
58+
````OnChange
59+
Actual value: @thePercentage, formatted value @thePercentage.ToString("P2")
60+
61+
<br />
62+
63+
<TelerikNumericTextBox @bind-Value="@thePercentage" Format="P2" Decimals="4" OnChange="@ChangePercentage" />
64+
65+
@code{
66+
double thePercentage { get; set; } = 12;
67+
68+
void ChangePercentage(object currValue)
69+
{
70+
// implement the desired logic here - it depends on the values you expect
71+
// for example, this app expects percentages between 0% and 100%, so it divides by 100
72+
// note that this is a heuristic task because 1000% may be a valid value
73+
// and it is usually up to the user to determine what they need and want
74+
if (thePercentage >= 1)
75+
{
76+
thePercentage = thePercentage / 100;
77+
}
78+
}
79+
}
80+
````
81+
````ValueChanged
82+
Actual value: @thePercentage, formatted value @thePercentage.ToString("P2")
83+
84+
<br />
85+
86+
<TelerikNumericTextBox Value="@thePercentage" Format="P2" Decimals="4" ValueChanged="@( (double v) => ChangePercentage(v) )" />
87+
88+
@code{
89+
double thePercentage { get; set; } = 12;
90+
91+
void ChangePercentage(double currValue)
92+
{
93+
// implement the desired logic here - it depends on the values you expect
94+
// for example, this app expects percentages between 0% and 100%, so it divides by 100
95+
// note that this is a heuristic task because 1000% may be a valid value
96+
// and it is usually up to the user to determine what they need and want
97+
// note 2: ValueChanged fires on every keystroke and is more invasive to the UX
98+
if (currValue >= 1)
99+
{
100+
thePercentage = currValue / 100;
101+
}
102+
else
103+
{
104+
thePercentage = currValue;
105+
}
106+
}
107+
}
108+
````
109+

0 commit comments

Comments
 (0)