Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Right align currency in string format

I'm trying to align some currency to the right:

double number1 = 150.45;
double number2 = 1400.95;

//Output  kr. 150,45
Console.WriteLine("{0:c2}", number1);

//Output  kr. 1.400,95
Console.WriteLine("{0:c2}", number2);

But I want my output to look like this.

//Output kr.   150.45

//Output kr. 1.400,95

Where the number is aligned to the right?

like image 214
gulbaek Avatar asked Sep 14 '11 20:09

gulbaek


2 Answers

it's rather hard for the system to say how many places your numbers have. So you have to decide this yourself. If you have decided you can use something like String.PadLeft

For example

Console.WriteLine("kr. {0}", number1.ToString("#,##0.00").PadLeft(10,' '));
like image 119
Random Dev Avatar answered Sep 22 '22 02:09

Random Dev


 string sym = CultureInfo.CurrentCulture.NumberFormat.CurrencySymbol;
 Console.WriteLine("{0}{1,10:#,##0.00}",sym, number1);
 Console.WriteLine("{0}{1,10:#,##0.00}",sym, number2);

ideone output

like image 25
Bala R Avatar answered Sep 25 '22 02:09

Bala R