How do I truncate double like 99.998765 to something like 99.99 instead of 100.00? If some printf("%.2f", myDouble); gives 100
That is not what I want.
Any help is appreciated
Stefano Mtangoo
455
Senior Poster
Recommended Answers
Jump to PostYou mean something like std::setprecision ?
Jump to PostIn that case you could use a custom function which truncates:
#include <iostream> #include <string> #include <sstream> std::string truncate(double d, unsigned decimals) { std::stringstream sstr; sstr << d; std::string d_str = sstr.str(); return d_str.substr(0, d_str.find_first_of(".") + decimals + 1); } int main () { double d = …
All 6 Replies
Nick Evan
4,005
Industrious Poster
Team Colleague
Featured Poster
Stefano Mtangoo
455
Senior Poster
Nick Evan
4,005
Industrious Poster
Team Colleague
Featured Poster
Stefano Mtangoo
commented:
Cool Man, I was wondering and you gave a cane
+4
chiwawa10
77
Junior Poster
Nick Evan
4,005
Industrious Poster
Team Colleague
Featured Poster
Stefano Mtangoo
455
Senior Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.