Decimal stores precision from parsed string in C#? What are the implications?

后端 未结 3 2102
萌比男神i
萌比男神i 2021-02-07 11:10

During a conversation on IRC, someone pointed out the following:

decimal.Parse(\"1.0000\").ToString() // 1.0000
decimal.Parse(\"1.00\").ToString() // 1.00
         


        
3条回答
  •  春和景丽
    2021-02-07 11:38

    In addition to post I see here, I would personally add a side note:

    always during persistence manipulations with floating point/decimal/double numbers consider the culture you're in, or you're going to save in. The code like here written is first, but definitive pass to complete mess and non culture independent architecture.

    Use Decimal.Parse (String, IFormatProvider).

    In my opinion, that methods (Parse From/To) that lack of Culture parameter have to be removed from the library to force a developer to think about that very important aspect.

提交回复
热议问题