C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old September 19th, 2004, 09:15 PM
brix brix is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 1 brix User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Post c++(how to convert decimal numbers to nearest integer

how to convert decimal numbers to nearest interger in C++.thanks.
waiting 4 reply.

-brix

Reply With Quote
  #2  
Old September 20th, 2004, 02:40 AM
kode_monkey kode_monkey is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 367 kode_monkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 21 sec
Reputation Power: 15
You can use the std lib functions round and trunc. Trunc will just remove the decimal part. Round will return the closest integer (rounding up for 0.5 or above and down otherwise).

Hope this helps,

-KM-

Reply With Quote
  #3  
Old February 24th, 2009, 01:51 PM
Amira Amira is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2009
Posts: 1 Amira User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 m 14 sec
Reputation Power: 0
rounding to the nearest integer

you can try the following simple program;


#include <iostream>
using namespace std;
int main()
{
double A;
cin>>A;
cout<<static_cast<int>(A+0.5)<<endl;
return 0;
}

Reply With Quote
  #4  
Old July 13th, 2010, 10:25 AM
Frenske Frenske is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2010
Posts: 2 Frenske User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 32 m 10 sec
Reputation Power: 0
This is wrong, well it is right for positive numbers, but not for negative numbers. For example int (-13.6 + 0.5) = -13, whereas the answer should be -14.

Quote:
Originally Posted by Amira
you can try the following simple program;


#include <iostream>
using namespace std;
int main()
{
double A;
cin>>A;
cout<<static_cast<int>(A+0.5)<<endl;
return 0;
}

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > c++(how to convert decimal numbers to nearest integer


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap