Giter Site home page Giter Site logo

parsefloat's Introduction

Parse a string to a float

The function strtodouble converts a string to a double similar to atof. It is written in C99 and only depends on libc.

Why?

strtodouble is a locale-independent version of atof and strtodouble. atof and strtod respect the locale which means that the digit point is locale-dependent. While in some applications this is exactly what you want, it is not suited for reading floats from data files or configurations files. Also, changing the environment and thus changing the locale is not always possible.

Usage

The prototype of the function is:

double strtodouble(const char *str, int *success)

The string you want to parse is given as the pointer str. The parsing stops at the first invalid character. The function returns the parsed value or NAN (not a number) if an error occured. If success is not NULL, at exit success will be 1 if the conversion was successfull or 0 if an error occured. Note that checking success is the only way to tell the difference between an error and parsing a NAN.

Limitations

There is a number of limitations:

  • Hexadecimal floats are not supported.
  • Rounding-off errors may occur; this is most likely not an issue in most cases.
  • The function calls pow which might be slow.

Testing

For tests see tests.c.

parsefloat's People

Contributors

michael-hartmann avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.