3

I got for you today a simple problem that already took me part of the day with no real achievement. I'm trying to format a number to display as a currency. There's many ways to do that, I think I've done most of 'em.

So here's the problem: I'm adding multiple number which can be positive and negative, I can't know. When I'm formatting at the end using TextBox.Text = Format(variable, "C"), I got the right format for positive numbers (which is per example 123 456,00 $, I live in Canada) and I have (123 456,00 $) for negative ones. I would prefer to have the "-" symbol at the beginning.

I searched the web to find other ways to do that, per example : FormatCurrency("-123 456", 2, TriState.True, TriState.False). That way I'm able to get rid of the parentheses but the "-" is AFTER the number (123 456,00 $-).

Next one: SpecificCulture. so here I have NegativeValueHere.ToString("C", CultureInfo.CreateSpecificCulture("fr-CA")) . With that I'm back at the beginning with my parentheses with (123 456,00 $). Note here that my application is running with culture set to ("fr-CA") since that's where I live.

I tried one more but can't remember what it was.However the "$" was in front of the number like $-123 456,00.

Note: I'm running on VB.Net, and the number has to be formatted to be put in a readonly TextBox.

2
  • you want to get a value displayed as "-$123 456,00" instead of "$-123 456,00", is that correct?
    – Csharp
    Mar 29, 2013 at 19:33
  • Nah I want the $ symbol after the number. But the answer from nkvu fits my needs. Thanks for awsering.
    – Simon
    Mar 29, 2013 at 19:58

2 Answers 2

4

You can create your own culture with a custom pattern for negative currency values. Take a look at the MSDN library article for NumberFormatInfo.CurrencyNegativePattern. For example:

Imports System.Globalization

Module Module1
    Sub Main()
        Dim simon = DirectCast(CultureInfo.GetCultureInfo("fr-CA").Clone, CultureInfo)
        simon.NumberFormat.CurrencyNegativePattern = 1
        Dim test = -1234.567
        Console.WriteLine(test.ToString("C", simon))
        Console.ReadLine()
    End Sub
End Module

Output: -$1 234,57

1
  • Thanks for the tip, @Hans. I was not aware of that.
    – Csharp
    Mar 29, 2013 at 20:02
3

This seems to work for me:

Sub Main()
    Dim value As Decimal = -123.45
    Dim positiveValue As Decimal = 123.45
    Dim customCurrencyInfo As CultureInfo = CultureInfo.CreateSpecificCulture("fr-CA")

    customCurrencyInfo.NumberFormat.CurrencyNegativePattern = 8

    Dim formatString As String = value.ToString("C", customCurrencyInfo)
    Dim formatStringPositive As String = positiveValue.ToString("C", customCurrencyInfo)

    Console.WriteLine(formatString) '-123,45 $
    Console.WriteLine(formatStringPositive) '123,45 $
    Console.ReadLine()

End Sub

You can get the different pattern values for NumberFormat.CurrencyNegativePattern from this link.

Sorry if I'm off track

3
  • Nah that totally works. I only tried the NumberFormat.CurrencyNegativePattern on DataGridView's column and it didn't work right away with TextBox so I didn't go any further. The pattern number 8 is the one I needed. Thank you.
    – Simon
    Mar 29, 2013 at 19:56
  • hey hey. what about if we want to get the other countries format? @nkvu? where could we get the complete list if let say i want to use malaysian format / any other asian currency format?
    – gumuruh
    Apr 29, 2014 at 1:42
  • @gumuruh Run the test program provided in the link from the answer.
    – mbomb007
    Aug 17, 2016 at 13:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.