C# Currency to string

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

    The Money data type has a precision of 4 decimal places. You'll need to either specify the format in the ToString() arguments or you can round off the value before converting to a string.

    If you use .ToString() you're not getting any rounding as far as I know. I think you just lose the digits.

    If you round, you're not just chopping off digits. Here's a sample for rounding (untested code):

    string value = Math.Round(ReturnValue, 2).ToString();
    

提交回复
热议问题