-1

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?

Ivan Stoev
  • 195,425
  • 15
  • 312
  • 343
  • *but is there a way to insert a decimal point in the string?* yes, `string.Insert` – Selvin Oct 21 '19 at 10:47
  • 4
    parse amount as decimal (instead of int), divide by 100 and then format. – phuzi Oct 21 '19 at 10:47
  • @Selvin how can I set it that it will put decimal before the last 2 digit? – Jose Baleros Jr. Oct 21 '19 at 10:49
  • Possible duplicate of [C# adding a character in a string](https://stackoverflow.com/questions/3879710/c-sharp-adding-a-character-in-a-string) – Selvin Oct 21 '19 at 10:53
  • @Selvin LOL i forgot, my brain is exhausted tsk tsk. Thanks. I'll try this one. – Jose Baleros Jr. Oct 21 '19 at 10:53
  • Is it _always_ US Dollars? While most currencies do use 2 decimals, there are some that use 3 and I believe also 0, if I remember correctly. So you might want to take a closer look at Dmitry's answer. – Fildor Oct 21 '19 at 10:59
  • @Fildor: That answer is pretty much useless for the scenario you describe. Assuming there's some other info as to what currency the number is in, that answer will only ever format it in the currency of the server. Even if you co-located it into multiple regions, each with a different server culture, the value should not be formatted based on whatever culture the server happens to be in. – Chris Pratt Oct 21 '19 at 16:19
  • @ChrisPratt Hi, we are using a SAP and I created an app that will convert the text file report from bank to my app then the output will be a LSMW for sap to batch input. actually I successfully uploaded the output text file using this answer. – Jose Baleros Jr. Oct 22 '19 at 00:21
  • @ChrisPratt That's correct. Cannot edit the comment any more, though. Btw: I agree with your preferation of a numerical solution rather than textual. – Fildor Oct 22 '19 at 06:12

2 Answers2

3

First convert the string to decimal and apply string.Format in this way

string.Format("{0:#.00}", Convert.ToDecimal(bankString) / 100);

this will give the result 100.50

https://dotnetfiddle.net/WrRVFo

Prasad Telkikar
  • 15,207
  • 5
  • 21
  • 44
  • 1
    In general, this is the better way to handle it (convert to decimal, and then correct the decimal point placement). However, I'd use `decimal.TryParse` instead, just in case there's bad data that can't be converted: `decimal.TryParse(original, out var d) ? (d / 100).ToString("0.00") : null`. – Chris Pratt Oct 21 '19 at 16:15
1

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');
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215