Search code examples
stringdoubledtypeconverterdmd

d programming, parse or convert string to double


as easy as it is in other languages, i can't seem to find an option in the d programming language where i can convert a string (ex: "234.32") into a double/float/real.

using atof from the std.c.stdio library only works when i use a constant string. (ex: atof("234.32") works but atof(tokens[i]); where tokens is an dynamic array with strings doesn't work).

how to convert or parse a string into a real/double/float in the d-programming language?


Solution

  • Easy.

    import std.conv;
    import std.stdio;    
    
    void main() {
        float x = to!float("234.32");
        double y = to!double("234.32");
    
        writefln("And the float is: %f\nHey, we also got a double: %f", x, y);
    }
    

    std.conv is the swiss army knife of conversion in D. It's really impressive!