locked
Format price with 2 decimals in format of user language RRS feed

  • Question

  • User-1246604461 posted
    Im trying to return a value which I store in cents to the user.

    So a value in the database of 34390 should be returned as 343.90 or 343,90 (. or , depending on the language of the user).

    So I created this function:

    Public Shared Function FormatPrice(ByVal price As String, ByVal lang As String) As String 'formats the price on front-end
    Dim culture As CultureInfo = New CultureInfo(lang)
    Threading.Thread.CurrentThread.CurrentCulture = culture
    Threading.Thread.CurrentThread.CurrentUICulture = culture

    Return String.Format("{0:0.00}", price / 100)
    End Function


    But it returns "343,9"

    Why is the trailing 0 not added?
    Thursday, October 6, 2011 4:42 PM

Answers

  • User-276842610 posted

    Hi Peter,

    Please try to check the following method:

    float doubleNumber = ((float)34390 / 100);  // 343.9
    
    Console.WriteLine(doubleNumber.ToString("F2", CultureInfo.CurrentCulture));

    reference: http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx 

    Hope this helps, thanks.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 12, 2011 4:44 AM

All replies

  • User1759999623 posted

    Some currencies do not use the second decimal place if it's a zero.  What language/currency are you using here?

    Thursday, October 6, 2011 4:50 PM
  • User-1246604461 posted

    Languages would be Dutch, German and English.
    But IMO I just try to convert an integer to a number with 2 decimal places. The culture should then determine if the decimal separator is a , or a . right?
    So what next? :)

     

    Friday, October 7, 2011 12:54 PM
  • User-1246604461 posted

    Anyone? Any suggestions?

    Tuesday, October 11, 2011 11:40 AM
  • User-276842610 posted

    Hi Peter,

    Please try to check the following method:

    float doubleNumber = ((float)34390 / 100);  // 343.9
    
    Console.WriteLine(doubleNumber.ToString("F2", CultureInfo.CurrentCulture));

    reference: http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx 

    Hope this helps, thanks.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 12, 2011 4:44 AM