C# Currency to string

后端 未结 10 2851
旧巷少年郎
旧巷少年郎 2021-02-19 10:52

I am querying a database field that returns a money value, I am assigning this to a string but it is adding extra 00 on the end.

e.g. Query returns 30.00

相关标签:
10条回答
  • 2021-02-19 11:33

    I am currently developing a boatdock web system, using VS 2013 Community edition. I was trying to work out how to output a currency value into a textbox.

    The method that I used was

    fieldName.Text = decimalValue.ToString("c");

    It works perfectly.

    0 讨论(0)
  • 2021-02-19 11:33

    In SQLServer a money data type is the equivalent of a decimal with 4 decimal places of precision. I would assume this is precision is conserved when the decimal is converted to a string. The best option would be to always use a format string eg "#,##0.00" when doing the conversion.

    0 讨论(0)
  • 2021-02-19 11:35

    I'd use something like

    string value = ReturnValue.ToString("0.00");
    

    This uses the ToString overload that accepts a format string. The above format string "0.00" specifies two decimal places.

    0 讨论(0)
  • 2021-02-19 11:35

    My recommendation is to convert it to Decimal firstly. I'd use something like this:

    string value = (Convert.ToDecimal(ReturnValue)).ToString("0,0.00");
    
    0 讨论(0)
提交回复
热议问题