String to Float, without introducing errors

Chris Angelico rosuav at gmail.com
Sat Dec 17 16:27:21 EST 2022


On Sun, 18 Dec 2022 at 08:22, Grant Edwards <grant.b.edwards at gmail.com> wrote:
>
> On 2022-12-17, Chris Angelico <rosuav at gmail.com> wrote:
>
> >> It was the rounding rounding error that I needed to avoid (as Peter
> >> J. Holzer suggested). The use of decimal solved it and just in
> >> time. I was about to truncate the number, get each of the
> >> characters from the string mantissa, and then do something like
> >> this:
> >>
> >> 64550.727
> >>
> >> 64550 + (7 * 0.1) + (2 * 0.01) + (7 * 0.001)
> >>
> >> Now I do not need to!
> >
> > It sounds like fixed-point arithmetic might be a better fit for what
> > you're doing.
>
> Yes, fixed point (or decimal) is a better fit for what he's doing. but
> I suspect that floating point would be a better fit for the problem
> he's trying to solve.
>

Hard to judge, given how little info we have on the actual problem.
Could go either way.

ChrisA


More information about the Python-list mailing list