Right align currency in string format

16,282

Solution 1

 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

Solution 2

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,' '));

Solution 3

This should work for any culture:

int width = 20;

string result = 1400.95.ToString("C");

NumberFormatInfo nfi = Thread.CurrentThread.CurrentCulture.NumberFormat;
if (nfi.CurrencyPositivePattern % 2 == 0)
{
    result = nfi.CurrencySymbol +
             result.Substring(nfi.CurrencySymbol.Length).PadLeft(width);
}
else
{
    result = result.PadLeft(width + nfi.CurrencySymbol.Length);
}

// result == "$            1,400.95" (en-US)
//           "£            1,400.95" (en-GB)
//           "           1.400,95 €" (de-DE)
//          "           1.400,95 kr" (sv-SE)
//         "              1.401 kr." (is-IS)
//          "kr            1 400,95" (nb-NO)
//         "kr.            1.400,95" (da-DK) (!)

Solution 4

Sure this isn't as good as the accepted answer, but I had the same problem with right-alignment and my immediate reflex was to:

//Output  kr. 150,45
Console.WriteLine("{0,10}",string.Format("{0:c2}",number1));
Share:
16,282

Related videos on Youtube

gulbaek
Author by

gulbaek

Updated on June 04, 2022

Comments

  • gulbaek
    gulbaek almost 2 years

    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?

  • Feidex
    Feidex over 12 years
    What if the culture puts the currency symbol after the number?
  • Bala R
    Bala R over 12 years
    @dtb you can look at NumberFormatInfo.CurrencyPositivePattern and reformat it I guess.
  • gulbaek
    gulbaek over 12 years
    Your first suggestion is missing the thousandsseparator, and your second suggestion is not working. See ideone.com/NQuHs
  • Feidex
    Feidex over 12 years
    What if the culture formats currency values not with two decimal places or separates thousands not in groups of 3?
  • Feidex
    Feidex over 12 years
    This globalization stuff is so difficult to get right... maybe one could try to insert spaces between currency symbol and digits after formatting the value as currency.
  • Bala R
    Bala R over 12 years
    @dtb {0:c2} in OPs snippet corresponds to two decimal places and that's what I have in my snippet. If it's not two decimal places, the format string has to be changed accordingly. Again, NumberFormatInfo has all the info for current culture; in this case it would be NumberFormatInfo.CurrencyDecimalDigits
  • gulbaek
    gulbaek over 12 years
    At the moment, I'm going for this answers. The system is only working with one currency.