-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NumericUpDown StringFormat doesn't handle percentages correctly. #3376
Comments
I have an issue in the same direction. We use Numerics string format to display inline units for inputs. Having a string format containing a "p" or "P" results in a display of percentage |
I've investigated this issue and these are my findings: The current implementation in NumericUpDown seems to have special handling of format specifiers such as P, % and ‰. The .NET formatting rules equals 1 to "100%" or "1000%" using the P specifier. In case these format specifiers are used in NumericUpDown the value will be divided in such a way that the .NET formatting rules will be ignored. Example: It seems to me that @SteffanDonal is expecting the default behaviour in .NET but this is not how NumericUpDown works right now. I can see the usefulness of both variations of how to format P, % and ‰ We could support both variations by adding a property to NumericUpDown such as I can provide a PR for this if we want to make NumericUpDown support both the current custom formatting of P, % and ‰ and the native formatting provided by the framework. @ckaeser-lsi your issue is actually a different bug where NumericUpDown formatting does not take into account where the specifier is used. I have a fix for your issue and will submit it as a PR. Not sure if @punker76 wants us to raise a separate issue and submit the PR there or here. Case 3 & 4 above is related to this issue. |
Hi @mgnslndh , did you get the standard .NET-Method to work? I tried to change the behaviour for myself in the past, but then i got some strange behaviour. I thought that this was the reason for this implementation. Since then I use a converter which works quite nice. Happy coding, |
@timunie I haven't tried making it work like .NET default formatting yet. But I guess there might be some issues to work through. @punker76 have we decided that the current behavior is a bug? This is my perception of this issue. Is it correct? Current behavior Using the P format specifier and entering a value of 100 should result in a value of 100. Desired behavior Using the P format specifier and entering a value of 100 should result in a value of 1. For users that still want the current behavior they could change the formatting to use something like "{}{0} %" which gives them the percentage sign but does not affect the value. |
@mgnslndh From my point of view I can confirm that this is what I was expecting when I used the control the first time. But I don't mind if it will not be fixed. Happy coding |
@mgnslndh @timunie I think we should change the behavior to the .Net default formatting. This should not confuse new users and doesn't make anything unusual you wouldn't expect.
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
Console.WriteLine($">> value: 100 => P => {100:P}");
Console.WriteLine($">> value: 1 => P => {1:P}");
Console.WriteLine($">> value: 0.1 => P => {0.1:P}");
Console.WriteLine($">> value: 0.01 => P => {0.01:P}");
Console.WriteLine();
Console.WriteLine($">> value: 100 => % => {100}%");
Console.WriteLine($">> value: 1 => % => {1}%");
Console.WriteLine($">> value: 0.1 => % => {0.1}%");
Console.WriteLine($">> value: 0.01 => % => {0.01}%");
Console.WriteLine();
Console.WriteLine($">> value: 100 => 0% => {100:0%}");
Console.WriteLine($">> value: 1 => 0% => {1:0%}");
Console.WriteLine($">> value: 0.1 => 0% => {0.1:0%}");
Console.WriteLine($">> value: 0.01 => 0% => {0.01:0%}");
Console.WriteLine();
Console.WriteLine($">> value: 100 => 0.0% => {100:0.0%}");
Console.WriteLine($">> value: 1 => 0.0% => {1:0.0%}");
Console.WriteLine($">> value: 0.1 => 0.0% => {0.1:0.0%}");
Console.WriteLine($">> value: 0.01 => 0.0% => {0.01:0.0%}");
Console.WriteLine();
Console.WriteLine($">> value: 100 => 0.000% => {100:0.000%}");
Console.WriteLine($">> value: 1 => 0.000% => {1:0.000%}");
Console.WriteLine($">> value: 0.1 => 0.000% => {0.1:0.000%}");
Console.WriteLine($">> value: 0.01 => 0.000% => {0.01:0.000%}");
}
} |
@mgnslndh @timunie The same with ‰
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
Console.WriteLine($">> value: 100 => ‰ => {100}‰");
Console.WriteLine($">> value: 1 => ‰ => {1}‰");
Console.WriteLine($">> value: 0.1 => ‰ => {0.1}‰");
Console.WriteLine($">> value: 0.01 => ‰ => {0.01}‰");
Console.WriteLine();
Console.WriteLine($">> value: 100 => 0‰ => {100:0‰}");
Console.WriteLine($">> value: 1 => 0‰ => {1:0‰}");
Console.WriteLine($">> value: 0.1 => 0‰ => {0.1:0‰}");
Console.WriteLine($">> value: 0.01 => 0‰ => {0.01:0‰}");
Console.WriteLine();
Console.WriteLine($">> value: 100 => 0.0‰ => {100:0.0‰}");
Console.WriteLine($">> value: 1 => 0.0‰ => {1:0.0‰}");
Console.WriteLine($">> value: 0.1 => 0.0‰ => {0.1:0.0‰}");
Console.WriteLine($">> value: 0.01 => 0.0‰ => {0.01:0.0‰}");
Console.WriteLine();
Console.WriteLine($">> value: 100 => 0.000‰ => {100:0.000‰}");
Console.WriteLine($">> value: 1 => 0.000‰ => {1:0.000‰}");
Console.WriteLine($">> value: 0.1 => 0.000‰ => {0.1:0.000‰}");
Console.WriteLine($">> value: 0.01 => 0.000‰ => {0.01:0.000‰}");
}
} |
Hi @punker76 , what will happen if the user enters 100 and omits the % when using Happy coding |
The current behavior for e.g. percentage text input with StringFotmat like P0 or {0:0.00%} converts the input one-to-one to the value: 100% = 100 This is not what the user expects, because the .Net default works different: 100% = 1 So this PR changes this behavior to the .Net default one.
Make sure you have a test for the following combination StringFormat={}{0}% This would be how you get the percentage sign without any conversion. Other than that, looking good @punker76 |
|
The current behavior for e.g. percentage text input with StringFotmat like P0 or {0:0.00%} converts the input one-to-one to the value: 100% = 100 This is not what the user expects, because the .Net default works different: 100% = 1 So this PR changes this behavior to the .Net default one.
The current behavior for e.g. percentage text input with StringFotmat like P0 or {0:0.00%} converts the input one-to-one to the value: 100% = 100 This is not what the user expects, because the .Net default works different: 100% = 1 So this PR changes this behavior to the .Net default one.
There's strange behaviour happening with
NumericUpDown
'sStringFormat
property, particularly around percentages.First it looks like the
P
,%
&‰
format specifiers are being ignored, but when adding those same symbols outside of a format item, the numbers appear to be divided by 100 or 1000.I do remember more frustrating issues before however - particularly with editing values. Previously, the numbers would look correct initially. After editing, the real value would be multiplied repeatedly.
To Reproduce:
Case 1:
NumericUpDown.StringFormat
to{0:0.00%}
or0.00%
NumericUpDown.Value
to0.25
0.25%
(expected25.00%
)Case 2:
NumericUpDown.StringFormat
to{0:0.00‰}
or0.00‰
NumericUpDown.Value
to0.25
0.25‰
(expected250.00%
)Case 3:
NumericUpDown.StringFormat
to{0:0.0000}%
NumericUpDown.Value
to0.25
0.0025%
(expected0.2500%
)Case 4:
NumericUpDown.StringFormat
to{0:0.00000}‰
NumericUpDown.Value
to0.25
0.00025‰
(expected0.25000‰
)Case 5:
NumericUpDown.StringFormat
toP
NumericUpDown.Value
to0.25
0.25%
(expected25.00 %
)Environment:
The text was updated successfully, but these errors were encountered: