C# Currency to string

后端 未结 10 2854
旧巷少年郎
旧巷少年郎 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:20

    MartGriff,

    My best advice would be to convert it to a double using the SqlMoney type. From there, you can output it however you would like!

    Here's an example:

    System.Data.SqlTypes.SqlMoney ReturnValue;
    
    //Set your returnValue with your SQL statement
    ReturnValue = ExecuteMySqlStatement();
    
    //Get the format you want
    
    //$30.00
    string currencyFormat = ReturnValue.ToDouble().ToString("c");
    
    //30.00
    string otherFormat = ReturnValue.ToDouble().ToString("0.00");
    

    For more formatting options, check out the MSDN:

    http://msdn.microsoft.com/en-us/library/system.double.tostring.aspx

    Best of luck, I hope this helps.

提交回复
热议问题