【问题标题】:Issue while reading file data [duplicate]读取文件数据时出现问题[重复]
【发布时间】:2016-05-08 14:38:29
【问题描述】:

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

我尝试使用 File.ReadAllText 以及 reader.ReadToEnd() ,其中 reader 是 StreamReader 实例。我也尝试指定编码(UTF8)。没有任何效果。

【问题讨论】:

  • Please check the encoding of the file 告诉我们它是什么。
  • 它显示编码为ANSI
  • @Thomas:ANSI 不是 UTF8!如需快速修复,请将文件重新保存为 UTF8。
  • @JesseGood:谢谢。我尝试使用 Encoding.Default,它现在对我有用。
  • 我明白了。 Encoding.Default 在您的系统上使用了通常有效的默认编码。在命令提示符下,键入 chcp 以验证您的系统默认代码页。通常是 1252,但例如我的是 932。

标签: c# asp.net .net


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2016-05-02
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多