Cannot parse "7,345.55" to 7344.55 maui blazor

Kalyan A 440 Reputation points
2025-03-05T19:39:18.5233333+00:00

I am not able parse Close value example close has "7,324.55"

its is giving as 4550.

    string clsd = columns[8].Replace("\"", "");
 bool isCloseParsed = false; var close = 0.0f;
                if (clsd.Contains(","))
                {
                    string newclsd = clsd.Replace(",", "");
                    isCloseParsed = float.TryParse(newclsd,  out close);
                   
                }
                else
                {
                    isCloseParsed = float.TryParse(clsd, out  close);
                }

Included sample data

@page "/NSEInd"
@using Microsoft.AspNetCore.Components.Forms
@using System.Globalization
@using Microsoft.ML
@using Microsoft.ML.Data
@using Microsoft.ML.Transforms.TimeSeries
@inject IJSRuntime JSRuntime
@using System.Text.Json
<h3>Upload CSV File</h3>
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,982 questions
0 comments No comments
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 49,126 Reputation points Microsoft External Staff
    2025-03-06T06:31:57.17+00:00

    Hello,

    After testing your code, there is no problem with the way the number is parsed. 7,344.55 can be converted to 7344.55 and displayed in the Blazor page. This should be a problem when you set the Blazor page code variable.

    You can refer to the following runnable code.

    
    <p role="status">@close</p>
    
     
    
    <button class="btn btn-primary" @onclick="@Replace">Click me</button>
    
     
    
    @code {
    
        private string str = "7,344.55";
    
        private float close;
    
        private void Replace()
    
        {
    
            bool isCloseParsed = false;
    
            if (this.str.Contains(","))
    
            {
    
                string newclsd = this.str.Replace(",", "");
    
                isCloseParsed = float.TryParse(newclsd, out this.close);
    
     
    
            }
    
            else
    
            {
    
                isCloseParsed = float.TryParse(this.str, out this.close);
    
            }
    
        }
    
    }
    
    

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.