Why does str() round up floats?
问题 The built-in Python str() function outputs some weird results when passing in floats with many decimals. This is what happens: >>> str(19.9999999999999999) >>> '20.0' I'm expecting to get: >>> '19.9999999999999999' Does anyone know why? and maybe workaround it? Thanks! 回答1: It's not str() that rounds, it's the fact that you're using floats in the first place. Float types are fast, but have limited precision; in other words, they are imprecise by design . This applies to all programming