locked
Format String as Currency RRS feed

  • Question

  • I want to take a string, say the following strings...and make them as what's shown on the right of each...

    .65                    $0.65

    87.9                  $87.90

    1208.04             $1,208.04

    18241.3             $18,241.30

    1.82                   $1.82

    So as you can see, I always want to display to two decimal points so you can see the cents, show the number before the decimal even if it's 0, add in digit grouping(the same name calculator gives it, adding in the commas...), and to show the currency symbol, in my case is U.S. dollars...

    I want it to be able to work in most cases, even if it still shows English, so it will show the symbol for euros, etc...depending on the PC settings...in my case negative would show as (8.92) instead of -$8.92 which is fine, if I hate it, I could change it on the PC...anyways, anyway to format the string as currency, and hopefully using the PC's settings which are actually very extensive now that I look at them...

    Wednesday, October 2, 2013 6:56 PM

Answers

  • You don't need to call the CurrencyFormatter directly. You can use a ToString conversion with the currency formatter:

    TextBlock2.Text = CDbl(MoneyAmount).ToString("C")

    See the ToString(String) docs for more information on the available formats.

    --Rob

    • Marked as answer by Maya Key Thursday, October 3, 2013 5:33 AM
    Thursday, October 3, 2013 1:59 AM
    Moderator

All replies

  • I found a really helpful guide for this...

    Link: http://www.c-sharpcorner.com/UploadFile/c25b6d/currency-formatter-class-in-windows-store-apps/

    My code is below...

    Dim a As New Windows.Globalization.NumberFormatting.CurrencyFormatter("USD")
    a.IsGrouped = True
    TextBlock2.Text = a.Format(CDbl(MoneyAmount))

    Now, this makes everything I want happen EXCEPT that I had to specify USD, I am going to provide a way to override it manually, but is there any way to do a conversion using the currency/currency symbol set by the system as the default, that way I don't have to manually specify this, or maybe detect it, and then specify either EUR/USD by code after detecting what it's set to?

    EDIT: Also, I forgot I also had to specify IsGrouped to true, even though my PC currency settings have digit grouping every 3 digits, it wasn't done for me...is there anyway to detect these system settings & use it instead?

    • Edited by Maya Key Wednesday, October 2, 2013 7:36 PM
    Wednesday, October 2, 2013 7:23 PM
  • You don't need to call the CurrencyFormatter directly. You can use a ToString conversion with the currency formatter:

    TextBlock2.Text = CDbl(MoneyAmount).ToString("C")

    See the ToString(String) docs for more information on the available formats.

    --Rob

    • Marked as answer by Maya Key Thursday, October 3, 2013 5:33 AM
    Thursday, October 3, 2013 1:59 AM
    Moderator
  • The link isn't completely helpful, as "C" seems to follow all the rules set in the system, and not simply display as shown in the post...which is what I want, to follow all the rules set in the system so that they can get a consistent experience between my app and others, in the way that currency is displayed so if they want to change it, they only have to do so once in the system for all their apps to change, given they are programmed like that...and not displaying the same thing regardless of the users system settings. 
    Thursday, October 3, 2013 5:38 AM