question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NumericUpDown StringFormat doesn't handle percentages correctly.

See original GitHub issue

There’s strange behaviour happening with NumericUpDown’s StringFormat 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:

  1. Set NumericUpDown.StringFormat to {0:0.00%} or 0.00%
  2. Set NumericUpDown.Value to 0.25
  3. See that the label now shows 0.25% (expected 25.00%)

Case 2:

  1. Set NumericUpDown.StringFormat to {0:0.00‰} or 0.00‰
  2. Set NumericUpDown.Value to 0.25
  3. See that the label now shows 0.25‰ (expected 250.00%)

Case 3:

  1. Set NumericUpDown.StringFormat to {0:0.0000}%
  2. Set NumericUpDown.Value to 0.25
  3. See that the label now shows 0.0025% (expected 0.2500%)
  4. Additionally, editing this number causes further division to the real value.

Case 4:

  1. Set NumericUpDown.StringFormat to {0:0.00000}‰
  2. Set NumericUpDown.Value to 0.25
  3. See that the label now shows 0.00025‰ (expected 0.25000‰)
  4. Additionally, editing this number causes further division to the real value.

Case 5:

  1. Set NumericUpDown.StringFormat to P
  2. Set NumericUpDown.Value to 0.25
  3. See that the label now shows 0.25% (expected 25.00 %)

Environment:

  • MahApps.Metro version 2.0.0-alpha0083
  • OS: Win10 1803
  • Visual Studio 2017 15.7.1
  • .NET Framework 4.6.2

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:11 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
punker76commented, Nov 7, 2019

Make sure you have a test for the following combination

StringFormat={}{0}% Value=100 Text=100%

This would be how you get the percentage sign without any conversion.

Other than that, looking good @punker76

image

1reaction
punker76commented, Nov 6, 2019

@mgnslndh @timunie The same with ‰

>> value: 100  => ‰      => 100‰
>> value: 1    => ‰      => 1‰
>> value: 0.1  => ‰      => 0.1‰
>> value: 0.01 => ‰      => 0.01‰

>> value: 100  => 0‰     => 100000‰
>> value: 1    => 0‰     => 1000‰
>> value: 0.1  => 0‰     => 100‰
>> value: 0.01 => 0‰     => 10‰

>> value: 100  => 0.0‰   => 100000.0‰
>> value: 1    => 0.0‰   => 1000.0‰
>> value: 0.1  => 0.0‰   => 100.0‰
>> value: 0.01 => 0.0‰   => 10.0‰

>> value: 100  => 0.000‰ => 100000.000‰
>> value: 1    => 0.000‰ => 1000.000‰
>> value: 0.1  => 0.000‰ => 100.000‰
>> value: 0.01 => 0.000‰ => 10.000‰
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‰}");
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

NumericUpDown control for Percentages?
A quick and easy answer to this question: use DecimalUpDown from the Extended WPF Toolkit, instead of NumericUpDown (it should be noted that...
Read more >
NumericUpDown - ValueFormat as Pecentage
Hi,I have trouble when trying use the NumericUpDown control with the following configuration:ValueFormat = ValueFormat.Percentage ...
Read more >
Thread: Format String Textbox
To start with the % sign should never be part of the value assessed. You should actually be doing the complete opposite of...
Read more >
MahApps.Metro v2.0.0 release notes (2020-05-24) | LibHunt
... SnapToMultipleOfInterval property does not work with decimal increments; #3376 NumericUpDown StringFormat doesn't handle percentages correctly.
Read more >
Set the Format for NumericUpDown Control
Learn how to set the format for the Windows Forms NumericUpDown control, by means of code samples in Visual Basic, C#, and CPP....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found