By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,972 Members | 1,115 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,972 IT Pros & Developers. It's quick & easy.

how to find if a number contains a decimal

P: 4
is there a way to find a decimal in a umber similar to str$ in VB
Jun 15 '07 #1
Share this Question
Share on Google+
7 Replies


gpraghuram
Expert 100+
P: 1,275
is there a way to find a decimal in a umber similar to str$ in VB
r u asking how can we find whether the nuber is an integer or float?
Is my assumption right

Raghu
Jun 15 '07 #2

P: 4
no..i'm doing some math calculations and need to ..i think.. convert the results to a string or char so i can determine if there is a decimal point int the math result..maybe it can be done someway else???
Jun 15 '07 #3

gpraghuram
Expert 100+
P: 1,275
no..i'm doing some math calculations and need to ..i think.. convert the results to a string or char so i can determine if there is a decimal point int the math result..maybe it can be done someway else???
If u are doing a math calculation then why ar u converting it to string.
If u think the math expression may give decimal then always use a float/double to store the values and convert it to int when needed.
My suggestion is dont convert the output to string and convert it back to intger or float.


Raghuram
Jun 15 '07 #4

Savage
Expert 100+
P: 1,764
no..i'm doing some math calculations and need to ..i think.. convert the results to a string or char so i can determine if there is a decimal point int the math result..maybe it can be done someway else???
I don't understand what are you trying to do.Can you give as a small example?

Savage
Jun 15 '07 #5

P: 4
i need to determine if a division result is a whole number or has a decimal in it.
Jun 15 '07 #6

ilikepython
Expert 100+
P: 844
i need to determine if a division result is a whole number or has a decimal in it.
I think the modulo operator might work:
Expand|Select|Wrap|Line Numbers
  1. int num1 = 24;
  2. int num2 = 6;
  3.  
  4. if (!(num1 % num2))      // the numbers divide evenly
  5. {
  6.     cout << "The result is a whole number." << endl;
  7. }
  8. else
  9. {
  10.     cout << "The result has a decimal in it." << endl;
  11. }
  12.  
Jun 15 '07 #7

weaknessforcats
Expert Mod 5K+
P: 9,197
Use a stringstream.

Expand|Select|Wrap|Line Numbers
  1. double d = 1.2345;
  2. stringstream ss;
  3. string str;
  4.  
  5. ss << d;
  6. ss >> str;
  7.  
Now you can locate the decimal point inside the string usingfind().
Jun 15 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.