0

I am using C# (.NET 4.5) to read a text file which has some financial information. However, whenever I read negative values (ex: -$ 1,000.00) from the file, I see that the "-$" is converted to "�". Ex: – $1,000.00 to � 1,000.00. Why is this happening?

I tried using File.ReadAllText as well as reader.ReadToEnd() where reader is a StreamReader instance. I tried specifying the Encoding as well (UTF8). Nothing worked.

Thomas
  • 1,970
  • 4
  • 28
  • 59
  • [Please check the encoding of the file](http://stackoverflow.com/questions/3710374/get-encoding-of-a-file-in-windows) and let us know what it is. – Jesse Good Jan 30 '16 at 23:50
  • It shows the encoding as ANSI – Thomas Jan 30 '16 at 23:54
  • @Thomas: ANSI is not UTF8! For a quick fix, resave the file as UTF8. – Jesse Good Jan 30 '16 at 23:56
  • @JesseGood: thank you. I tried using Encoding.Default and it works for me now. – Thomas Jan 30 '16 at 23:58
  • I see. `Encoding.Default` used the default encoding on your system which usually works. On the command prompt, type `chcp` to verify your system default code page. Often times its 1252, but for example mine is 932. – Jesse Good Jan 31 '16 at 00:03

1 Answers1

-1

You will need to format the string like this:

string curC = System.Threading.Thread.CurrentThread.CurrentCulture.ToString();
System.Globalization.NumberFormatInfo curFormat = new System.Globalization.CultureInfo(curC).NumberFormat;
curFormat.CurrencyNegativePattern = 1;

num.ToString("c", curFormat);
// or string.Format(curFormat, "{0:c}", num);
Vivek Verma
  • 333
  • 3
  • 13