. Net String.Format () do dodawania przecinków w tysiącach dla liczby
Chcę dodać przecinek w miejscu tysięcy dla liczby. String.Format()
?
20 answers
String.Format("{0:n}", 1234); //Output: 1,234.00
string.Format("{0:n0}", 9876); // no digits after the decimal point. Output: 9,876
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-12-07 16:42:14
Uznałem to za najprostszy sposób:
myInteger.ToString("N0")
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-05-18 13:48:00
int number = 1000000000;
string whatYouWant = number.ToString("#,##0");
//You get: 1,000,000,000
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-05-19 17:26:35
Jeśli chcesz specyficznej kultury, możesz spróbować tego:
(19950000.0).ToString("N",new CultureInfo("en-US"))
= 19,950,000.00
(19950000.0).ToString("N",new CultureInfo("is-IS"))
= 19.950.000,00
Uwaga: Niektóre Kultury używają ,
do oznaczenia dziesiętnego, a nie .
więc bądź ostrożny.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-12-23 11:18:22
Standardowe formaty, z ich powiązanymi wyjściami,
Console.WriteLine("Standard Numeric Format Specifiers");
String s = String.Format("(C) Currency: . . . . . . . . {0:C}\n" +
"(D) Decimal:. . . . . . . . . {0:D}\n" +
"(E) Scientific: . . . . . . . {1:E}\n" +
"(F) Fixed point:. . . . . . . {1:F}\n" +
"(G) General:. . . . . . . . . {0:G}\n" +
" (default):. . . . . . . . {0} (default = 'G')\n" +
"(N) Number: . . . . . . . . . {0:N}\n" +
"(P) Percent:. . . . . . . . . {1:P}\n" +
"(R) Round-trip: . . . . . . . {1:R}\n" +
"(X) Hexadecimal:. . . . . . . {0:X}\n",
- 1234, -1234.565F);
Console.WriteLine(s);
Przykładowe wyjście (kultura en-us):
(C) Currency: . . . . . . . . ($1,234.00)
(D) Decimal:. . . . . . . . . -1234
(E) Scientific: . . . . . . . -1.234565E+003
(F) Fixed point:. . . . . . . -1234.57
(G) General:. . . . . . . . . -1234
(default):. . . . . . . . -1234 (default = 'G')
(N) Number: . . . . . . . . . -1,234.00
(P) Percent:. . . . . . . . . -123,456.50 %
(R) Round-trip: . . . . . . . -1234.565
(X) Hexadecimal:. . . . . . . FFFFFB2E
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-12-12 16:19:17
To najlepszy format. Działa we wszystkich tych przypadkach:
String.Format( "{0:#,##0.##}", 0 ); // 0
String.Format( "{0:#,##0.##}", 0.5 ); // 0.5 - some of the formats above fail here.
String.Format( "{0:#,##0.##}", 12314 ); // 12,314
String.Format( "{0:#,##0.##}", 12314.23123 ); // 12,314.23
String.Format( "{0:#,##0.##}", 12314.2 ); // 12,314.2
String.Format( "{0:#,##0.##}", 1231412314.2 ); // 1,231,412,314.2
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-07-09 23:43:49
String.Format("{0:#,###,###.##}", MyNumber)
To da ci przecinki w odpowiednich punktach.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-05-11 18:24:22
Jeśli chcesz wymusić separator "," niezależnie od kultury (na przykład w wiadomości trace lub log), poniższy kod będzie działał i ma dodatkową zaletę w postaci poinformowania następnego gościa, który natknie się na niego dokładnie, co robisz.
int integerValue = 19400320;
string formatted = string.Format(CultureInfo.InvariantCulture, "{0:N0}", integerValue);
Zestawy sformatowane na "19,400,320"
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-03-27 19:55:29
Najczęściej głosowana odpowiedź była świetna i była pomocna od około 7 lat. Wraz z wprowadzeniem C# 6.0, a konkretnie interpolacji ciągów znaków, istnieje lepszy i, IMO bezpieczniejszy, sposób na zrobienie tego, o co poproszono to add commas in thousands place for a number
:
var i = 5222000;
var s = $"{i:n} is the number"; // results to > 5,222,000.00 is the number
s = $"{i:n0} has no decimal"; // results to > 5,222,000 has no decimal
Gdzie zmienna i
jest umieszczona w miejscu elementu zastępczego (tj. {0}
). Więc nie ma potrzeby, aby pamiętać, który obiekt idzie do której pozycji. Formatowanie (tj. :n
) nie uległo zmianie. Aby zapoznać się z pełną funkcjonalnością Nowości, możesz [8]} przejść do tego Strona .
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-08-12 02:51:49
Poniższy przykład wyświetla kilka wartości, które są sformatowane za pomocą niestandardowych ciągów formatujących zawierających zero elementów zastępczych.
String.Format("{0:N1}", 29255.0);
Lub
29255.0.ToString("N1")
Wynik "29,255. 0"
String.Format("{0:N2}", 29255.0);
Lub
29255.0.ToString("N2")
Wynik "29 255,00"
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-03-07 12:11:31
Po prostu takie proste:
float num = 23658; // for example
num = num.ToString("N0"); // Returns 23,658
Więcej informacji znajduje się w tutaj
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-08-24 05:08:32
int num = 98765432;
Console.WriteLine(string.Format("{0:#,#}", num));
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-05-20 15:36:29
Na przykład String.Format("{0:0,0}", 1);
zwraca 01, dla mnie nie jest poprawne
To działa dla mnie
19950000.ToString("#,#", CultureInfo.InvariantCulture));
Wyjście 19,950,000
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-10-23 21:19:57
Zauważ, że formatowana wartość powinna być liczbowa. Nie wygląda na to, że będzie to ciąg reprezentacji Liczby i format jest z przecinkami.
Prostsze, używając interpolacji string zamiast String.Format
$"{12456:n0}"; // 12,456
$"{12456:n2}"; // 12,456.00
Lub używając yourVariable
double yourVariable = 12456.0;
$"{yourVariable:n0}";
$"{yourVariable:n2}";
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-11-02 20:22:05
Możesz użyć funkcji takiej jak ta do formatowania liczb i opcjonalnie przekazywania żądanych miejsc po przecinku. Jeśli nie podano miejsc po przecinku, będzie on używał dwóch miejsc po przecinku.
public static string formatNumber(decimal valueIn=0, int decimalPlaces=2)
{
return string.Format("{0:n" + decimalPlaces.ToString() + "}", valueIn);
}
Używam dziesiętnych, ale można zmienić typ na dowolny inny lub użyć obiektu anonimowego. Można również dodać sprawdzanie błędów pod kątem ujemnych wartości miejsca dziesiętnego.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-12-23 11:13:38
String.Format("0,###.###"); also works with decimal places
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-02-13 12:20:52
Metoda, której użyłem, aby nie martwić się już kulturami i potencjalnymi problemami formatowania, polega na tym, że sformatowałem ją jako walutę i wyjąłem symbol waluty później.
if (decimal.TryParse(tblCell, out result))
{
formattedValue = result.ToString("C").Substring(1);
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-06-13 09:55:01
Poniżej jest jednak dobre rozwiązanie w Javie!
NumberFormat fmt = NumberFormat.getCurrencyInstance();
System.out.println(fmt.format(n));
Lub w bardziej solidny sposób możesz chcieć uzyskać ustawienia regionalne konkretnego miejsca, a następnie użyć jak poniżej:
int n=9999999;
Locale locale = new Locale("en", "US");
NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
System.out.println(fmt.format(n));
US Locale OUTPUT: $9,999,999.00
Niemiecki Locale output
Locale locale = new Locale("de", "DE");
Wyjście: 9.999.999,00 €
Indian Locale output
Locale locale = new Locale("de", "DE");
Wyjście: Rs.9,999,999.00
Estoński Locale wyjście
Locale locale = new Locale("et", "EE");
Wyjście: 9 999 999 €
Jak widać na różnych wyjściach nie musisz się martwić o to, że separator będzie przecinkiem lub kropką lub nawet spacją możesz uzyskać liczbę sformatowaną zgodnie ze standardami i18n
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-12-22 06:06:01
Jeśli chcesz wyświetlić go w DataGridview, powinieneś zmienić jego typ, ponieważ domyślnie jest to String, a ponieważ zmienisz go na dziesiętny, uważa on za liczbę z zmiennoprzecinkową
Dim dt As DataTable = New DataTable
dt.Columns.Add("col1", GetType(Decimal))
dt.Rows.Add(1)
dt.Rows.Add(10)
dt.Rows.Add(2)
DataGridView1.DataSource = dt
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-11-28 03:36:47