Skip to content
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

InputNumber formatting for decimal numbers #60112

Open
wants to merge 16 commits into
base: main
Choose a base branch
from

Conversation

vertonghenb
Copy link
Contributor

@vertonghenb vertonghenb commented Jan 30, 2025

InputNumber with formatting (double, decimal, float)

Summary of the changes
InputNumber with formatting as in 0.00 or #.00 etc.

Description

In <InputDate we can choose how to format the date using the types. e.g. mm/yyyy etc. With InputNumber this is not possible to format decimal numbers. This PR intends to format the InputNumber with floating point numbers.

Scope

The following are in scope:

  • Types:
    • double
    • float
    • decimal
  • Components:
    • InputNumber

API

<InputNumber @bind-Value="A" Format="0.00"/>

In this case the decimal will always have 2 trailing zero's.

<InputNumber @bind-Value="A" Format="0.0000"/>

In this case the decimal will always have 4 trailing zero's.

Other possible formats:

Input Value Format String Expected Output Description
"1.1" "0.0#" "1.1" Single decimal place with optional second
"1500" "0.00" "1500.00" Force two decimal places
"1500" "0.0000" "1500.0000" Force four decimal places
"1500" "0.##" "1500" Remove unnecessary decimals
"0" "0.00" "0.00" Zero with fixed decimals
"0" "0.##" "0" Zero with optional decimals
"-1.1" "0.0#" "-1.1" Negative number with one decimal place
"-1500" "0.00" "-1500.00" Negative number with two fixed decimals
"1.999" "0.0" "2.0" Rounding up
"1.111" "0.0" "1.1" Rounding down
"1234567.89" "N2" "1,234,567.89" Large number with thousands separator
"1234567.89" "#,##0.00" "1,234,567.89" Explicit thousands separator format
"0.1234" "0.00%" "12.34%" Percentage formatting
"0.12" "00.00" "00.12" Fixed zero's with fixed decimals
"1234567.89" "0.00" "1234567.89" Fixed two decimals

Notes:

  • However it will work in the tests but the type="number" on the input might stop us from showing thousand separators. since 1.000,01 won't show in the type="number". I don't think we want to change the type to text since we might break existing usage.

Any idea's how to solve this problem are welcome. The other formats will work as expected.

Idea's

Haven't tested it but using inputmode and pattern might accomplish the same:

<input type="text" pattern="^[0-9․,]+$" inputmode="decimal"/>

Fixes #30567

@vertonghenb vertonghenb requested a review from a team as a code owner January 30, 2025 16:59
@dotnet-issue-labeler dotnet-issue-labeler bot added the area-blazor Includes: Blazor, Razor Components label Jan 30, 2025
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Jan 30, 2025
Copy link
Contributor

Thanks for your PR, @vertonghenb. Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

/// <summary>
/// Gets or sets the format to be used when displaying a number of types: <see cref="float"/>, <see cref="double"/>, <see cref="decimal"/>.
/// </summary>
[Parameter] public string? Format { get; set; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

@javiercn javiercn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a good starting point, but it needs E2E tests. I'm not confident the format bit will work with InputNumber since it creates an underlying <input type="number" /> element, which I believe doesn't take any format as it's a number value in JS

@javiercn javiercn added the pr: pending author input For automation. Specifically separate from Needs: Author Feedback label Feb 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components community-contribution Indicates that the PR has been added by a community member pr: pending author input For automation. Specifically separate from Needs: Author Feedback
Projects
None yet
Development

Successfully merging this pull request may close these issues.

FormatStrings not working with @bind-value:format for <InputNumber> in Blazor WebAssembly
2 participants