Форматирование Чисел как Строки с Запятыми вместо Десятичных чисел

Проверьте, заблокировано ли соединение брандмауэром или нет. Если сторонний брандмауэр активен, он может заблокировать TCP-соединение. После этого шага -
1. Убедитесь, что TCP включен или нет
a. Откройте диспетчер конфигурации SQL Server
b. Конфигурация сети SQL Server
c. Протоколы для MSSQLSERVER
d. TCP - включите его
e. Также проверьте прослушивание всех - Да
f. На вкладке IP-адреса IPAll - номер порта должен быть 1433

13
задан John Saunders 5 May 2010 в 18:25
поделиться

9 ответов

I think:

string.Format(System.Globalization.CultureInfo.GetCultureInfo("de-DE"), "{0:0.0}", 4.3); 

should do what you want.

31
ответ дан 1 December 2019 в 17:12
поделиться
NumberFormatInfo nfi = new NumberFormatInfo();
nfi.NumberDecimalSeparator = ",";
nfi.NumberGroupSeparator = ".";

double num = 4.3;
string ret = num.ToString(nfi);    // 4,3
31
ответ дан 1 December 2019 в 17:12
поделиться

This depends on what you want to do. If you want to make your entire application ready for some other language output just use

System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo ("de-DE");

If you want to have a mixed language application (that means some of the outputs are formatted in one language some in another) you have to use the overloads of the specific String functions like:

var culture =  System.Globalization.CultureInfo.GetCultureInfo("de-DE");   
String.Format (culture, "{0:0.0}", 4.3);

The first method is preferred if it is possible because you only need one single line in your application and everything will be as expected. In the second you have to add the culture parameter to every String ouput method in your entire application.

11
ответ дан 1 December 2019 в 17:12
поделиться

Yes, you are using the wrong string, and also the problem can't be solved by only providing a formatting string.

What your formatting string does is to format the number using the pattern "0", then aligned to the length 0.

When you specify the decimal separator in a formatting string it's always a period regardless of the current culture. The decimal separator in the result of the formatting on the other hand is always the one used by the current culture. So, to get a comma as decimal separator in the result you have to make sure that the culture used for the formatting is one that uses comma as decimal separator.

You can either set the current culture for the thread so that it's used by default by the formatting, or specify a culture in the call:

string ret = String.Format(CultureInfo.GetCultureInfo(1053), "{0:0.0}", 4.3);
1
ответ дан 1 December 2019 в 17:12
поделиться

Settings the thread culture will do this conversion automatically. However, if you want to format this with a custom string you can do the following:

string ret = string.Format("{0:0.00}", 4.3);

or

string ret = (4.3f).ToString("0.00");

The "." is the format specifier for decimal point for the current culture. More info for custom number formats are found at: http://msdn.microsoft.com/en-us/library/0c899ak8.aspx

You can always check what character for the decimal point is with:

string decimalChar = Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator;
0
ответ дан 1 December 2019 в 17:12
поделиться

Для более общего решения вы можете установить культуру пользовательского интерфейса потока на культуру, используемую вашими европейскими друзьями. Это повлияет на представление чисел, валюты, дат и т. Д.

3
ответ дан 1 December 2019 в 17:12
поделиться

Форматирование чисел по умолчанию использует NumberFormatInfo из текущего CultureInfo , чтобы оно отображалось с использованием региональных настроек на компьютере.

Следовательно, вам действительно не нужно делать с этим ничего особенного, кроме, возможно, проверки того, что используется правильный CultureInfo.

Что касается вопроса, да, строка недействительна. Знак "," обозначает разделитель тысяч, а не десятичный разделитель. Взгляните на NumberFormatInfo и пользовательские числовые форматы .

2
ответ дан 1 December 2019 в 17:12
поделиться

Number formatting can be handled for you by the framework if you use the correct culture when manipulating the number.

Console.WriteLine(4.3);

Console.WriteLine(4.3.ToString(CultureInfo.GetCultureInfo("fr-fr")));

Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("fr-fr");
Console.WriteLine(4.3);

If you want to do a "one-off" display, the second approach will work. If you want to display every number correctly, you really should be setting the current thread's culture. That way, any number manipulation will handle decimal separators, grouping characters and any other culture-specific things correctly.

The same goes for parsing numbers. If a user enters 1,234, how do you know whether they have entered 1.234 (the comma being the decimal separator) or 1234 (the comma being a grouping separator)? This is where the culture helps out as it knows how to display numbers and can also be used to parse them correctly:

Console.WriteLine(double.Parse("1,234"));

Console.WriteLine(double.Parse("1,234", CultureInfo.GetCultureInfo("fr-fr")));

Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("fr-fr");
Console.WriteLine(double.Parse("1,234"));

The above will output 1234 (the comma is a decimal separator in my en-us default culture), 1.234 (the comma is a decimal separator in French) and 1,234 (again, the comma is a decimal separator in French, and also the thread's culture is set To French so it displays using this culture - hence the comma as a decimal separator in the output).

0
ответ дан 1 December 2019 в 17:12
поделиться

Use a CultureInfo that has commas instead of decimal point (French for instance) :

string.Format(CultureInfo.GetCultureInfo("fr-FR"), "{0:0.0}", 4.3);
1
ответ дан 1 December 2019 в 17:12
поделиться
Другие вопросы по тегам:

Похожие вопросы: