locked
Remove Decimal Places From Currency Format RRS feed

  • Question

  • User-620468895 posted

    Does anoyone know how to remove the trailing zeros from a currency formated string:

    for example in the showad.aspx.c there is  a line which show the price of an item using this code:

    AdPriceLabel.Text = String.Format("{0:c}", ad.Price);

    if the value on the database table is 5 then it is diaplyed on the web page as:

     $5.00

     All the numbers in my datbase are whole numbers (no decimal places).

    Wednesday, November 22, 2006 10:59 AM

Answers

  • User-1580617485 posted

    Inspect the Current Number format System.Threading.Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalDigits; You can achieve the desired result as follows.

    System.Globalization.NumberFormatInfo nfi = new System.Globalization.NumberFormatInfo();

    nfi.CurrencyDecimalDigits = 0;

    nfi.CurrencySymbol = "$";

    AdPriceLabel.Text = String.Format(nfi,"{0:c}", ad.Price);

     

    Hope it helped.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 22, 2006 12:40 PM

All replies

  • User-1580617485 posted

    Inspect the Current Number format System.Threading.Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalDigits; You can achieve the desired result as follows.

    System.Globalization.NumberFormatInfo nfi = new System.Globalization.NumberFormatInfo();

    nfi.CurrencyDecimalDigits = 0;

    nfi.CurrencySymbol = "$";

    AdPriceLabel.Text = String.Format(nfi,"{0:c}", ad.Price);

     

    Hope it helped.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 22, 2006 12:40 PM
  • User-1580617485 posted
    Let us know if not resolved. Thanks
    Monday, November 27, 2006 2:41 PM
  • User-620468895 posted
    that worked great thanks.
    Monday, November 27, 2006 3:25 PM
  • User-620468895 posted
    that worked great thanks. is
    Monday, November 27, 2006 3:25 PM
  • User-620468895 posted
    that worked great thanks. is there any way to make
    Monday, November 27, 2006 3:25 PM
  • User-620468895 posted
    that worked great thanks. is there any way to
    Monday, November 27, 2006 3:25 PM
  • User-620468895 posted
    that worked great thanks. is there any way to make that a
    Monday, November 27, 2006 3:25 PM
  • User-620468895 posted
    that worked great thanks. is there
    Monday, November 27, 2006 3:25 PM
  • User-620468895 posted
    that worked great thanks. is there any way to make that
    Monday, November 27, 2006 3:25 PM
  • User-620468895 posted
    that worked great thanks. is there any way
    Monday, November 27, 2006 3:25 PM
  • User-620468895 posted
    that worked great thanks. is there any way to make that a global setting in say
    Monday, November 27, 2006 3:25 PM
  • User-620468895 posted
    that worked great thanks. is there any way to make that a global setting in
    Monday, November 27, 2006 3:25 PM
  • User-620468895 posted
    that worked great thanks. is there any way to make that a global setting in say the
    Monday, November 27, 2006 3:25 PM
  • User-620468895 posted
    that worked great thanks. is there any way to make that a global setting
    Monday, November 27, 2006 3:25 PM
  • User-620468895 posted
    that worked great thanks. is there any way to make that a global
    Monday, November 27, 2006 3:25 PM
  • User-620468895 posted
    wow, i don;t know what happened there but it posted a bout ten times.
    Monday, November 27, 2006 3:27 PM
  • User-1580617485 posted

    Yes we can. The section How to: Set the Culture and UI Culture for ASP.NET Web Page Globalization provides the overview.

    And Update the thread status thanks.

    Monday, November 27, 2006 3:42 PM
  • User1272303263 posted

    Inspect the Current Number format System.Threading.Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalDigits; You can achieve the desired result as follows.

    System.Globalization.NumberFormatInfo nfi = new System.Globalization.NumberFormatInfo();

    nfi.CurrencyDecimalDigits = 0;

    nfi.CurrencySymbol = "$";

    AdPriceLabel.Text = String.Format(nfi,"{0:c}", ad.Price);

     

    Hope it helped.

     

    --------------------------------------------------------------

    Hello,

    Can you tell me, where is this code located exactly. I need to make the changes as above, but i am unable to trace the location of the code.

    I am unable to find the file that carries this code.

    Help me please.

    Thanks in advance.

    Paggy4u

    Monday, November 27, 2006 9:43 PM
  • User-1580617485 posted

    There is no file. The code is right there. Please explain what you are trying to achieve?

    Thanks.

    Tuesday, November 28, 2006 10:33 AM
  • User1272303263 posted

    There is no file. The code is right there. Please explain what you are trying to achieve?

    Thanks.

     

    Thats what i am trying to achieve, I acannot locate the code in this huge classified starterkit.

    I just want to know, where is this code located. ( It must be inside some aspx or aspx.vb file )

    I just want to know that file name.


    Thanks

    Tuesday, November 28, 2006 10:13 PM
  • User-620468895 posted

    copme on paggy, don't be lazy and use a text search tool, the more you help yourself the more you'll learn.

    showad.aspx

    remember "Help comes to those who help themselves"

    Wednesday, November 29, 2006 6:58 AM
  • User1272303263 posted
    Hi,

    I was more busy finding a better solution. ( More Active rather than being LAZY )

    I have removed the decimals in many places. i shall be placing the codes tomorrow.

    Thanks

    Paggy4u
    Wednesday, November 29, 2006 7:57 AM
  • User1137530663 posted

    Hi,
    I've encountered the same problem but is unable to get the above mentioned solution to work. My code is as below:

    Dim nfi As System.Globalization.NumberFormatInfo = New System.Globalization.NumberFormatInfo

    nfi.CurrencyDecimalDigits = 2
    nfi.CurrencySymbol = "$"

    Dim myAmt As String = "300.440000000000"
    Me.Label_Amount.Text = String.Format(nfi, "{0:c}", myAmt)

     

     The result is still same. The currency symbol wasn't even prefix to the string at all! Am I missing something here? Thanks in advance.
     

    Monday, December 18, 2006 2:29 AM
  • User-1580617485 posted
    Me.Label_Amount.Text = String.Format(nfi, "{0:c}", Convert.ToDecimal(myAmt))
    Thursday, December 21, 2006 10:30 AM
  • User1137530663 posted
    I see. Thanks.
    Thursday, December 21, 2006 7:59 PM
  • User1108651750 posted

    If you use multiple cultures in your app, you might want to use the following to instantiate the NumberFormatInfo so that the current culture is used (instead of just constructing one with 'new'):

     System.Globalization.NumberFormatInfo nfi = (System.Globalization.NumberFormatInfo)System.Globalization.NumberFormatInfo.CurrentInfo.Clone();

     

    Friday, May 2, 2008 6:03 PM
  • User1593022345 posted


    {0:c} default

    {0:c0} for no decimal

    {0:c3}for 3 decimal places and so on.


    add 1,2,3 any number after c for that many decimal places

    Friday, December 18, 2009 11:57 AM
  • User-1096630142 posted

    I would just like to point out to anyone else searching on this thread, that there is a better way of accomplishing this.

    String.Format("0:C0") will work as well.  The 0 after the C specifices the number of decimal places.


    Sunday, October 10, 2010 5:40 PM