RadzenNumeric divides by 10 when entering a formatted number with multiple decimal places #885

Closed
opened 2026-01-29 17:45:48 +00:00 by claunia · 2 comments
Owner

Originally created by @fieldsj23 on GitHub (Jun 14, 2023).

Describe the bug
If you have a RadzenNumeric set up with a Format (ex. Format="0.0000") and you enter a number with 0's in the decimal places. RadzenNumeric will divide your input by 10 upon entering or losing scope.

To Reproduce
Steps to reproduce the behavior:

  1. Go to https://blazor.radzen.com/numeric
  2. Scroll down to the Formatted value example
  3. Click on the first box that shows 0.0000
  4. Type 10.0000 and hit enter
  5. The error appears as the value is changed to 1.0000

Expected behavior
The value should be the value typed in unless there is a Min/Max to restrict it.

Screenshots
Prior to hitting Enter
image

After hitting Enter
image

Desktop (please complete the following information):

  • OS: Windows 10
  • Browser: Chrome and Edge
  • Version: chrome - 114.0.5735.110
    edge: 114.0.1823.43

Additional context

  • Entering 100.0000 leds to 10.0000
  • Entering 1.0000 seems to work
  • Seems to only fail if every number that is in the format is zero. So, if the format is 0.00, then any multiple of 10.00.
Originally created by @fieldsj23 on GitHub (Jun 14, 2023). **Describe the bug** If you have a RadzenNumeric set up with a Format (ex. Format="0.0000") and you enter a number with 0's in the decimal places. RadzenNumeric will divide your input by 10 upon entering or losing scope. **To Reproduce** Steps to reproduce the behavior: 1. Go to https://blazor.radzen.com/numeric 2. Scroll down to the Formatted value example 3. Click on the first box that shows 0.0000 4. Type 10.0000 and hit enter 5. The error appears as the value is changed to 1.0000 **Expected behavior** The value should be the value typed in unless there is a Min/Max to restrict it. **Screenshots** Prior to hitting Enter ![image](https://github.com/radzenhq/radzen-blazor/assets/133258881/1cafd525-a8fd-4fe1-a202-1581d0d9dd09) After hitting Enter ![image](https://github.com/radzenhq/radzen-blazor/assets/133258881/0482fe17-696a-409f-93af-21326791f984) **Desktop (please complete the following information):** - OS: Windows 10 - Browser: Chrome and Edge - Version: chrome - 114.0.5735.110 edge: 114.0.1823.43 **Additional context** - Entering 100.0000 leds to 10.0000 - Entering 1.0000 seems to work - Seems to only fail if every number that is in the format is zero. So, if the format is 0.00, then any multiple of 10.00.
Author
Owner

@fieldsj23 commented on GitHub (Jun 14, 2023):

Using Format="#.0000" is just a workaround. This causes you to lose your preceding 0 if only a decimal value is entered. Gives ".1200" instead of 0.1200.

@fieldsj23 commented on GitHub (Jun 14, 2023): Using Format="#.0000" is just a workaround. This causes you to lose your preceding 0 if only a decimal value is entered. Gives ".1200" instead of 0.1200.
Author
Owner

@Roblix711 commented on GitHub (Aug 23, 2023):

@enchev
Why did you close this? The issue still remains.

@fieldsj23
After looking through the RadzenNumeric source code, the issue is here:

    private string RemoveNonNumericCharacters(object value)
    {
        string valueStr = value as string;
        if (valueStr == null)
        {
            valueStr = value.ToString();
        }

        if (!string.IsNullOrEmpty(Format))
        {
            valueStr = valueStr.Replace(Format.Replace("#", "").Trim(), ""); // this is doing a bad replace. 
        }

        return new string(valueStr.Where(c => char.IsDigit(c) || char.IsPunctuation(c)).ToArray()).Replace("%", "");
    }

With an input of "10.000", and a format of "0.000", you can see it performs a text replace:
Replacing "0.000" in "10.000", resulting in a string of "1".

As for a fix: Implement your own ConvertValue to bypass this behavior.

<RadzenNumeric TValue="decimal" ConvertValue="MyConv" Format="0.0000" @bind-Value="@number"></RadzenNumeric>

@code {
    private decimal MyConv(string? input)
    {
        decimal.TryParse(input, out decimal result); 
        return result;
    }
}

This worked for me in preserving the format, without any losses of the value.
Hope this helps.

@Roblix711 commented on GitHub (Aug 23, 2023): @enchev Why did you close this? The issue still remains. @fieldsj23 After looking through the RadzenNumeric source code, the issue is here: ``` private string RemoveNonNumericCharacters(object value) { string valueStr = value as string; if (valueStr == null) { valueStr = value.ToString(); } if (!string.IsNullOrEmpty(Format)) { valueStr = valueStr.Replace(Format.Replace("#", "").Trim(), ""); // this is doing a bad replace. } return new string(valueStr.Where(c => char.IsDigit(c) || char.IsPunctuation(c)).ToArray()).Replace("%", ""); } ``` With an input of "10.000", and a format of "0.000", you can see it performs a text replace: Replacing "0.000" in "10.000", resulting in a string of "1". As for a fix: Implement your own **ConvertValue** to bypass this behavior. ``` <RadzenNumeric TValue="decimal" ConvertValue="MyConv" Format="0.0000" @bind-Value="@number"></RadzenNumeric> @code { private decimal MyConv(string? input) { decimal.TryParse(input, out decimal result); return result; } } ``` This worked for me in preserving the format, without any losses of the value. Hope this helps.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/radzen-blazor#885