" /> " /> "/>

How to convert a string to Double with all the numbers after the floating poing?

74 Views Asked by At

Lets say i have a string "53.430996", I want to convert it to a string and store all the values after the floating point I tried:

#include <iostream>
#include <bits/stdc++.h>
#include <string>
#include <stdlib.h>
using namespace std;
int main(){
   char* convertme="53.430996";
   double converted=atof(convertme);
   cout << converted;
   return 0;
}

But the output is 53.431 but i need 53.430996 so I can later cast it to a long long with

static cast < long long > (coord ∗ 100000)

So, I can get the value without the floating point 5343099

1

There are 1 best solutions below

0
Eljay On

Just need to make sure you specify the desired precision for fixed point.

#include <iomanip>
#include <iostream>
#include <string>

using std::cout;
using std::stod;
using std::fixed;
using std::setprecision;

int main(){
   auto convertme="53.430996";
   auto converted = stod(convertme);
   cout << fixed << setprecision(6) << converted << "\n";
}