有关转换数字格式的问题 [英] Question about convert number format
问题描述
我对这个话题感到非常沮丧.我有一个希腊文的文本文件,只包含数字.我的应用程序是c#应用程序,默认语言环境是英语.现在,我想将此希腊文本文件读入我的应用程序.由于两种数字格式不同,我无法获得正确的数字.有人可以告诉我如何将希腊数字格式转换为英语,然后可以使用我的应用程序吗?
Hi,
I am so frustrated about this topic. I have a text file from Greek, it only includes numbers. My application is c# application and default locale is english. Now I want to read this greek text file into my application. I cannot get correct numbers because of the two number formats are different. Could anybody tell me how to convert greek number format to english, then I can use my application?
any comment is highly appreciated!
推荐答案
我希望您理解数字数据本身没有文化差异,但是如果数字形式的文本可能具有表示形式, .例如,这样的差异之一是小数点.如果是这个问题?
在这种情况下,您只需要读取某些特定区域性的数据即可.
您可以在文本阅读器读取和解析数据之前更改当前线程的区域性:
I hope you understand that the numeric data itself has no cultural difference, but the representation if the numbers in the form of text may have. One such difference is a decimal point, for example. If this the problem?
In this case, you only need to read data in some specific culture.
You can change the culture of a current thread before your text reader reads and parses the data:
System.Globalization.CultureInfo savedCulture = System.Threading.Thread.CurrentThread.CurrentCulture;
try {
//the culture of Greek (Greece):
System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("el-GR");
System.Threading.Thread.CurrentThread.CurrentCulture = culture;
//read and parse data in a usual way
} finally {
System.Threading.Thread.CurrentThread.CurrentCulture = savedCulture;
}
本质上,所有将文本解析为数字数据时都将有所作为;因此,在细节层面上,您可以做
Essentially, all that makes a difference while parsing a text into a numeric data; so, on a detail level, you can do
// use the culture defined above as System.IFormatProvider
// as System.Globalization.CultureInfo implements System.IFormatProvider
double value = double.Parse(someText, culture); //will throw exception in the case of invalid format
// or
if (!double.TryParse(someText, out value)) { /* ... */ } //without throwing exception
// here, we assume someText representing a single number
由于您只有10位数字,我认为您逐位读取文件,并将每个数字解析为等效的英文翻译.
Since you have only 10 digits, I would think you read the file digit by digit and parse each digit into its equivalent English translation.
您必须从字符串的后端开始.
You have to start from the back-end of the string.
这篇关于有关转换数字格式的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!