c++ to float code example

Example 1: how to change a string to an float in c++

#include <iostream>
#include <string>

int main() {
    std::string str = "123.4567";

    // convert string to float
    float num_float = std::stof(str);

    // convert string to double
    double num_double = std::stod(str);

   std:: cout<< "num_float = " << num_float << std::endl;
   std:: cout<< "num_double = " << num_double << std::endl;

    return 0;
}

Example 2: int to float c++

int a = 3;
float b = (float)a;

Example 3: how to make a float in C++

public float whatYourFloatIsCalled = 0;
public float whatIsYourFloat;

Tags:

Cpp Example