Format number to currency using VB.Net

currency, formatting, vb.net

Solution

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

Problem

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`.

Original source