Change format of outcome of Sum

Borisoprit 371 Reputation points
2021-09-01T20:36:16.427+00:00
I make a simple calculation with some Entry's , that works but this is not the format i really am looking for.

Totaaltanks.Text = total.ToString("N0", CultureInfo.InvariantCulture);

The outcome is for example 10,245,458
The format that i am looking for is that the outcome is 10245,458
With 1 comma only .

Tryed al lot of options but cannot find the one i am looking for.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,296 questions
{count} votes

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 26,446 Reputation points Microsoft Vendor
    2021-09-02T07:03:48.573+00:00

    Hello,
    Welcome to our Microsoft Q&A platform!
    You could have a try to use String.Format and #, it can be formatted by "-""/""?" or some other character , but it can't be formatted by comma.

    MyEntry.Text = string.Format("{0:#######-###}", totle);  
    

    Because comma turns the number into thousandths. If you want to use comma, you could use NumberFormatInfo.

    double a = 12345678.876555;  
    int b = 99999;  
    double totle = a + b;  
    NumberFormatInfo nfo = new NumberFormatInfo();  
    nfo.CurrencyGroupSeparator = ",";  
    nfo.CurrencyGroupSizes = new int[] { 3, 5 };  
    nfo.CurrencySymbol = "";  
    MyEntry.Text = totle.ToString("c0", nfo);  
    

    In addition, you could insert , into string.

    //int a = 1;  
    //int b = 9;  
    int a = 1111;  
    int b = 99999999;  
    double totle = a + b;  
    string totleString = totle.ToString();  
    int length = totleString.Length;  
    MyEntry.Text = length>3?totleString.Insert(length-3,","): totleString;  
    

    For more information , you can refer to
    https://learn.microsoft.com/en-us/dotnet/api/system.string.format?view=net-5.0#Starting
    Best Regards,
    Wenyan Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.
    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 comments No comments

1 additional answer

Sort by: Most helpful
  1. Borisoprit 371 Reputation points
    2021-09-20T19:49:34.747+00:00

    Thanks Wenyan Zhang ,

    I used this

    int a = 1111;
     int b = 99999999;
     double totle = a + b;
     string totleString = totle.ToString();
     int length = totleString.Length;
     MyEntry.Text = length>3?totleString.Insert(length-3,","): totleString;
    
    0 comments No comments