读取文件数据时出现问题

本文关键字:问题 文件 数据 读取 | 更新日期: 2023-09-27 18:32:44

我正在使用C#(.NET 4.5)来读取包含一些财务信息的文本文件。但是,每当我从文件中读取负值(例如:-$ 1,000.00)时,我都会看到"-$"被转换为" "。例如: – $1,000.00 至 1,000.00。为什么会这样?

我尝试使用File.ReadAllText和阅读器。ReadToEnd(),其中 reader 是一个 StreamReader 实例。我也尝试指定编码(UTF8)。什么都没用。

读取文件数据时出现问题

您需要像这样格式化字符串:

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);