how to transform from double to int (rounded) ?
by using basic stuff ?
fadi_1234 -3 Light Poster
Recommended Answers
Jump to PostUse a cast?
C++ style:double a = 1.40; int x = static_cast<int>(a);
"C style" (still valid)
double a = 1.40; int x = (int)a;
You will lose all digits after the decimal and it doesn't round, just truncates.
Jump to Posti cant use these method
I suppose you could just assign the double value to an int, but the cast makes your intentions much clearer.
Jump to PostI'm not quite sure how to frame it in a way that it will be acceptable to your instructor.
Jump to PostUsing static_cast isn't really an advanced way. I mean, if you want the simplest then following the advice to simply assign it to an integer variable should suffice.
You can always add a comment explaining what it does, just to make your intentions clear.
All 13 Replies
jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster
fadi_1234 -3 Light Poster
jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster
fadi_1234 -3 Light Poster
jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster
fadi_1234 -3 Light Poster
fadi_1234 -3 Light Poster
jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster
Chilton 22 Junior Poster
fadi_1234 -3 Light Poster
jonsca 1,059 Quantitative Phrenologist Team Colleague Featured Poster
fadi_1234 -3 Light Poster
fadi_1234 -3 Light 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.