last 3 numbers
-
If i have this number: 100000000000 my code will read it like this:
if (rol > 99999999999) label1.Text = get3digit(rol) + " miliarde";
...
//where get3digit(rol) method is this:
int get3digit(Int64 myVal)
{
char mg1 = myVal.ToString()[0];
char mg2 = myVal.ToString()[1];
char mg3 = myVal.ToString()[2];
char[] chars = { mg1, mg2, mg3 };
string mg = new string(chars);
int i = int.Parse(mg);
return i;
}
//---RESULT is: "100 miliarde" (for this example) (but up to "999 miliarde");999999999999 (it means 999 miliarde) and is the Maximum value i will make the code. Now, I want to split this number by 3 LAST characters !!! So it will look like this in the end: "100 000 000 000" (yes, with spaces) Also for other LESS numbers than this one. 100 000 000 000 10 000 000 000 1 000 000 000 100 000 000 10 000 000 1 000 000 100 000 10 000 1 000 and this is the last value to insert the space. So 9 cases. I give multiple of 10 example here to be easy to understand, but it will be other numbers between 0 and 999 milliard. My idea is to use -char- as i did already, to separate the last numbers. But... maybe there is some funky way of doing it more easily? Or AUTOMATIC/ Default way? Thank you very much.
-
If i have this number: 100000000000 my code will read it like this:
if (rol > 99999999999) label1.Text = get3digit(rol) + " miliarde";
...
//where get3digit(rol) method is this:
int get3digit(Int64 myVal)
{
char mg1 = myVal.ToString()[0];
char mg2 = myVal.ToString()[1];
char mg3 = myVal.ToString()[2];
char[] chars = { mg1, mg2, mg3 };
string mg = new string(chars);
int i = int.Parse(mg);
return i;
}
//---RESULT is: "100 miliarde" (for this example) (but up to "999 miliarde");999999999999 (it means 999 miliarde) and is the Maximum value i will make the code. Now, I want to split this number by 3 LAST characters !!! So it will look like this in the end: "100 000 000 000" (yes, with spaces) Also for other LESS numbers than this one. 100 000 000 000 10 000 000 000 1 000 000 000 100 000 000 10 000 000 1 000 000 100 000 10 000 1 000 and this is the last value to insert the space. So 9 cases. I give multiple of 10 example here to be easy to understand, but it will be other numbers between 0 and 999 milliard. My idea is to use -char- as i did already, to separate the last numbers. But... maybe there is some funky way of doing it more easily? Or AUTOMATIC/ Default way? Thank you very much.
-
If i have this number: 100000000000 my code will read it like this:
if (rol > 99999999999) label1.Text = get3digit(rol) + " miliarde";
...
//where get3digit(rol) method is this:
int get3digit(Int64 myVal)
{
char mg1 = myVal.ToString()[0];
char mg2 = myVal.ToString()[1];
char mg3 = myVal.ToString()[2];
char[] chars = { mg1, mg2, mg3 };
string mg = new string(chars);
int i = int.Parse(mg);
return i;
}
//---RESULT is: "100 miliarde" (for this example) (but up to "999 miliarde");999999999999 (it means 999 miliarde) and is the Maximum value i will make the code. Now, I want to split this number by 3 LAST characters !!! So it will look like this in the end: "100 000 000 000" (yes, with spaces) Also for other LESS numbers than this one. 100 000 000 000 10 000 000 000 1 000 000 000 100 000 000 10 000 000 1 000 000 100 000 10 000 1 000 and this is the last value to insert the space. So 9 cases. I give multiple of 10 example here to be easy to understand, but it will be other numbers between 0 and 999 milliard. My idea is to use -char- as i did already, to separate the last numbers. But... maybe there is some funky way of doing it more easily? Or AUTOMATIC/ Default way? Thank you very much.
Hi, you can specify how a number gets formatted on output without performing any char or string manipulation yourself. The basic mechanism consists of calling the ToString() method while providing a format pattern as well as an object of type
NumberFormatInfo
that holds your specific wishes. In this case it would be:NumberFormatInfo nfi = (NumberFormatInfo)CultureInfo.InvariantCulture.NumberFormat.Clone();
nfi.NumberGroupSeparator = " ";
int yourNumber=123456789;
string yourFormattedNumber = yourNumber.ToString("#,0", nfi);Obviously you need to create nfi only once, you can reuse it as often as you want. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
-
Int64 i = 100000000; Console.WriteLine( i.ToString( "### ### ###" ).TrimStart() );
"(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal
-
If i have this number: 100000000000 my code will read it like this:
if (rol > 99999999999) label1.Text = get3digit(rol) + " miliarde";
...
//where get3digit(rol) method is this:
int get3digit(Int64 myVal)
{
char mg1 = myVal.ToString()[0];
char mg2 = myVal.ToString()[1];
char mg3 = myVal.ToString()[2];
char[] chars = { mg1, mg2, mg3 };
string mg = new string(chars);
int i = int.Parse(mg);
return i;
}
//---RESULT is: "100 miliarde" (for this example) (but up to "999 miliarde");999999999999 (it means 999 miliarde) and is the Maximum value i will make the code. Now, I want to split this number by 3 LAST characters !!! So it will look like this in the end: "100 000 000 000" (yes, with spaces) Also for other LESS numbers than this one. 100 000 000 000 10 000 000 000 1 000 000 000 100 000 000 10 000 000 1 000 000 100 000 10 000 1 000 and this is the last value to insert the space. So 9 cases. I give multiple of 10 example here to be easy to understand, but it will be other numbers between 0 and 999 milliard. My idea is to use -char- as i did already, to separate the last numbers. But... maybe there is some funky way of doing it more easily? Or AUTOMATIC/ Default way? Thank you very much.
This will work regardless of the type of number, and regardless of its value.
long number = 100000000000;
string value = string.Format("{0:#,##0}", number).Replace(",", " ");".45 ACP - because shooting twice is just silly" - JSOP, 2010
-----
You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
-----
When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013 -
If i have this number: 100000000000 my code will read it like this:
if (rol > 99999999999) label1.Text = get3digit(rol) + " miliarde";
...
//where get3digit(rol) method is this:
int get3digit(Int64 myVal)
{
char mg1 = myVal.ToString()[0];
char mg2 = myVal.ToString()[1];
char mg3 = myVal.ToString()[2];
char[] chars = { mg1, mg2, mg3 };
string mg = new string(chars);
int i = int.Parse(mg);
return i;
}
//---RESULT is: "100 miliarde" (for this example) (but up to "999 miliarde");999999999999 (it means 999 miliarde) and is the Maximum value i will make the code. Now, I want to split this number by 3 LAST characters !!! So it will look like this in the end: "100 000 000 000" (yes, with spaces) Also for other LESS numbers than this one. 100 000 000 000 10 000 000 000 1 000 000 000 100 000 000 10 000 000 1 000 000 100 000 10 000 1 000 and this is the last value to insert the space. So 9 cases. I give multiple of 10 example here to be easy to understand, but it will be other numbers between 0 and 999 milliard. My idea is to use -char- as i did already, to separate the last numbers. But... maybe there is some funky way of doing it more easily? Or AUTOMATIC/ Default way? Thank you very much.
public string HowManyMillion(int millions, string suffix) => $"{millions / 1000000} {suffix}";
// use example: string millions = HowManyMillion(100000000, "miliarde");
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot