dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.52k stars 10.04k forks source link

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

Open fingers10 opened 3 years ago

fingers10 commented 3 years ago

Describe the bug

Display Formatting doesn't work with decimal types in Blazor WebAssembly.

To Reproduce

  1. Create a New Blazor Web Assembly App.
  2. Navigate to Index.razor
  3. Paste the below code.
<EditForm Model="@exampleModel" OnValidSubmit="@HandleValidSubmit">
    <DataAnnotationsValidator />
    <ValidationSummary />

    <InputNumber id="name" @bind-Value="exampleModel.Price" @bind-Value:format="{0:#.##}" />

    <button type="submit">Submit</button>
</EditForm>

@code
{
    protected ExampleModel exampleModel = new ExampleModel
    {
        Price = 580.471m
    };

    protected void HandleValidSubmit()
    {
    }

    public class ExampleModel
    {
        //[DisplayFormat(DataFormatString = "{0:#.##}", ApplyFormatInEditMode = true)]
        public decimal Price { get; set; }
    }
}
  1. Notice the output as shown below: image

  2. @bind-value:format doesn't seem to work. I have also tried with [DisplayFormat(DataFormatString = "{0:#.##}", ApplyFormatInEditMode = true)] Data Annotation. This also doesn't work.

  3. Later from the docs I noticed that

Data binding works with DateTime format strings using @bind:format. Other format expressions, such as currency or number formats, aren't available at this time.

Is there any plans on including this in preview release? Please can you share an update on this? Or is there any best workaround for this at the moment?

Further technical details

ghost commented 3 years ago

Thanks for contacting us. We're moving this issue to the Next sprint planning milestone for future evaluation / consideration. We will evaluate the request when we are planning the work for the next milestone. To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

ghost commented 3 years ago

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

TanayParikh commented 3 years ago

Hello @fingers10. As you mentioned from the docs:

Data binding works with a single DateTime format string using @bind:format="{FORMAT STRING}", where the {FORMAT STRING} placeholder is the format string. Other format expressions, such as currency or number formats, aren't available at this time but might be added in a future release.

This is not currently planned, but we'll use this issue to track community interest for this feature.

stewart-james commented 2 years ago

My company is keen for bind format string support against numeric types, we currently have to do the formatting ourselves whenever we want specific precision on our floating point values. Being able to bind directly to the double with a format string would be great, especially since Blazor / the browser would then handle the localisation for us :)

harveytriana commented 6 days ago

<input type="number" @bind-value="@number" @bind-value:format="0.000000" />

Error (active) CS1503 Argument 1: cannot convert from 'double' to 'System.DateTime' ...

Very bad.