Search code examples
c#text-formattingasp.net-core-mvc-2.0

How to put decimal point in a whole number


I'm reading a text file from bank with a string of numbers. 0000000010050, the amount of this string is $ 100.50, but is there a way to insert a decimal point in the string? here is my code.

string amount = 0000000010050; 
string f_amount = "";
string ff_amount = "";
decimal d_amount = 0;

f_amount = amount.Trim();
d_amount = int.Parse(f_amount.TrimStart('0')); // this part removes the zeros and the output is 10050.
ff_amount = string.Format("{0:0,0.00}", d_amount); // this line outputs 10050.00

how to make the output looks like this 100.50?


Solution

  • Something like this (let's take CultureInfo into account)

      using System.Globalization;
    
      ... 
    
      string amount = "0000000010050";
    
      amount = amount
        .Insert(amount.Length - CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalDigits,
                CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator)
        .TrimStart('0');