Here is an extract from my code:
#include <iostream>
#include <nlohmann/json.hpp>
#include <vector>
using nlohmann::json;
void fillJs(json js, std::vector<float>* positions)
{
positions->push_back(js[0]);
positions->push_back(js[1]);
}
float precision(float f, int places)
{
float n = std::pow(10.0f, places);
return std::round(f * n) / n;
}
int main()
{
json js{45652.012222222, 1563.4500001, 5.71235, 20218.9194556};
std::cout << "js [0] = " << js[0] << std::endl;
float yy = js[3];
std::cout << "yy = " << yy << std::endl;
std::cout << "js 2 round 1 = " << precision(js[3], 1) << std::endl;
std::cout << "js 2 round 2 = " << precision(js[3], 2) << std::endl;
std::cout << "js 2 round 3 = " << precision(js[3], 3) << std::endl;
std::vector<float> vec;
vec.push_back(js[0]);
std::cout << "vec 0 = " << vec[0] << std::endl;
std::vector<float> vec1;
fillJs(js, &vec1);
std::cout << "vec 1 = " << vec1[1] << std::endl;
}
And here is the output:
js [0] = 45652.012222222
yy = 20218.9
js 2 round 1 = 20218.9
js 2 round 2 = 20218.9
js 2 round 3 = 20218.9
vec 0 = 45652
vec 1 = 1563.45
Why is the float number rounded when assigned ant not rounded wen printed ?
I need to assign the values without rounding them, any idea please ?