How to display Number with comma in following format in C#.
Ex: 12345678 I want to display this number as 1,23,45,678
Can any one give answer for this. Thanks.
How to display Number with comma in following format in C#.
Ex: 12345678 I want to display this number as 1,23,45,678
Can any one give answer for this. Thanks.
As I understand, this is an Indian style of formatting the currency.
Console.WriteLine(intValue.ToString("N1", CultureInfo.CreateSpecificCulture("hi-IN")));
where intValue is the number you want to format.
For the same input in your question, you should get exactly "1,23,45,678".
You may try this:-
Double number = 12345678;
String output = number.ToString("C2");
Check out the Currency Formatter.
The "C" (or currency) format specifier converts a number to a string that represents a currency amount. The precision specifier indicates the desired number of decimal places in the result string. If the precision specifier is omitted, the default precision is defined by the NumberFormatInfo.CurrencyDecimalDigits property.
You can use Custom Format The Currency Format Specifier C
or c
using ToString()
From MSDN: Custom Format c
or C
Result: A currency value.
Supported by: All numeric types.
Precision specifier: Number of decimal digits.
Default precision specifier: Defined by System.Globalization.NumberFormatInfo.
Try This:
int myVal= 12345678;
string finalVal= myVal.ToString("C");
Console.WriteLine(finalVal);
Try This: if you dont want precision.
int myVal= 12345678;
string finalVal= myVal.ToString("C0");
Console.WriteLine(finalVal);
int number = 123456789;
string Result = number.ToString("#,##0");
Try this. Hope it helps
using System.Globalization;
NumberFormatInfo info = new NumberFormatInfo();
info.NumberGroupSizes = new int[] { 3, 2 };
int number = 12345678;
string Result = number.ToString("#,#", info);
here you Go...
you can use cultureinfo to show currency with your number
decimal dec = 123.00M;
string uk = dec.ToString("C", new CultureInfo("en-GB"); // uk holds "£123.00"
string us = dec.ToString("C", new CultureInfo("en-US"); // us holds "$123.00"
Well, a very dirty solution, but could not figure if there is a format for this.
So I take last 3, then by packs of two :
int number = 212345678;
string semiRes = number.ToString();
var lastThree = semiRes.Substring(semiRes.Length - 3, 3);
List<string> resulatArray = new List<string>();
resulatArray.Add(lastThree);
semiRes = semiRes.Substring(0, semiRes.Length - 3);
for (int i = 2; i <= semiRes.Length + 2; i = i + 2)
{
var start = semiRes.Length - i;
var len = 2;
if (start < 0)
{
len = 2 + start;
start = 0;
}
var nextTwo = semiRes.Substring(start, len);
resulatArray.Insert(0, nextTwo);
}
var result = string.Join(",", resulatArray);
if (result.StartsWith(","))
{
result = result.Substring(1);
}