views:

168

answers:

5

I like to format all numbers like in math. is there a predefined function or is that just possible with substring and replace?

edit: my culture is de-ch

Best regards

A: 

Try this:

Console.WriteLine(1000000.ToString("#,##0").Replace(
    CultureInfo.CurrentCulture.NumberFormat.NumberGroupSeparator, "'"));

Or

NumberFormatInfo likeInMath = new NumberFormatInfo()
{
    NumberGroupSeparator = "'"
};
Console.WriteLine(1000000.ToString("#,##0", likeInMath));
Rubens Farias
That doesn't work on my machine. It prints `1 000 000`. My culture is nb-NO
Svish
@Svish, just fixed that, take a look
Rubens Farias
A: 

use int.ToString() and an iFormatProvider.

also take a look here msdn.

runrunraygun
+3  A: 
var numformat = new NumberFormatInfo {
                   NumberGroupSeparator = "'",
                   NumberGroupSizes = new int[] { 3 },
                   NumberDecimalSeparator = "."
                };
Console.WriteLine(1000000.ToString("N",numformat));
adrianm
+4  A: 

Try this

int input = Convert.ToInt32("1700");
string result = String.Format("{0:##,##}", input);

Or this

Console.WriteLine(1700.ToString("##,##", new NumberFormatInfo() { NumberGroupSeparator = "'" })); 
Faisal
works perfectly, also with different cultures. thank you.
snarebold
A: 

I always Use this format

 "#,##0;#,##0'-';0"

so You can use it in

 int input = Convert.ToInt32("100000000");  
 string result = String.Format("{#,##0;#,##0'-';0}", input);
Nasser Hadjloo