0

I knew with command <<fixed << set precision() for command cout <<. But now I want to assign it, for example I have a float = 1.23456 and I want to have float b= 1.23. Can I use assign b by a with command fixed and set precision() ?. Thank you^^

Mark Dickinson
  • 29,088
  • 9
  • 83
  • 120
NamNguyen
  • 21
  • 6
  • 1
    Not all values are even possible to be stored as a float / double so you may not have the value you expect. Related: [https://stackoverflow.com/questions/588004/is-floating-point-math-broken](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – drescherjm Nov 29 '21 at 13:58
  • 1
    `float b = std::trunc(1.23456f * 100.0f) / 100.0f;`, q.v. [trunc](https://en.cppreference.com/w/cpp/numeric/math/trunc) or consider floor, ceil, or round. – Eljay Nov 29 '21 at 13:58
  • woahh, so advanced, I think it's easy, I's wrong , thank you very much<33 – NamNguyen Nov 29 '21 at 14:04

0 Answers0