11 Replies - 18006 Views - Last Post: 08 February 2009 - 10:58 PM Rate Topic: -----

#1 krisku  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 65
  • Joined: 13-December 08

How to get digits after decimal comma?

Post icon  Posted 06 February 2009 - 02:31 PM

Hello,

Maybe that's serial stupid question...

How can i get digits after decimal comma. I want to calculate sum of all digits in double number. For numbers before the comma code will look like that:


	while((int) n>0) {
		n/=10;
		i++;
	}



But I haven't got idea how to get digits after the comma. :ph34r:

Is This A Good Question/Topic? 0
  • +

Replies To: How to get digits after decimal comma?

#2 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: How to get digits after decimal comma?

Posted 06 February 2009 - 03:24 PM

I'd assume it has something to do with the modular (%).
Was This Post Helpful? 0
  • +
  • -

#3 krisku  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 65
  • Joined: 13-December 08

Re: How to get digits after decimal comma?

Posted 06 February 2009 - 04:25 PM

I can't find way to do that. If use modular aritmetic, compiler boom with error message: "Left operand has a double value". Other suggestions? :-)
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: How to get digits after decimal comma?

Posted 06 February 2009 - 05:38 PM

You don't need mod. You just... nm, here's the idea:
n = 1234
t = floor(n/10) = 123
(n - t*10) = (1234 - 1230) = 4


Was This Post Helpful? 0
  • +
  • -

#5 krisku  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 65
  • Joined: 13-December 08

Re: How to get digits after decimal comma?

Posted 07 February 2009 - 04:53 AM

View Postbaavgai, on 6 Feb, 2009 - 04:38 PM, said:

You don't need mod. You just... nm, here's the idea:
n = 1234
t = floor(n/10) = 123
(n - t*10) = (1234 - 1230) = 4



Maybe I don't asking question rightly. I want to count sum of digits AFTER decimal comma, e.x.:

152.25 ==> 1-2-5 (haven't got problem for calc them), but i don't know how to calculate 2-5.

With your code wrote this, but this isn't what i need. ;-)
Was This Post Helpful? 0
  • +
  • -

#6 Lillefix  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 37
  • View blog
  • Posts: 204
  • Joined: 19-September 08

Re: How to get digits after decimal comma?

Posted 07 February 2009 - 05:17 AM

AFAIK the fmod function in math.h is able to do decimal values.

Try fmod(num, 1) and see.

Edit: yeah, that should work

This post has been edited by Lillefix: 07 February 2009 - 05:17 AM

Was This Post Helpful? 0
  • +
  • -

#7 Plus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 41
  • View blog
  • Posts: 414
  • Joined: 24-November 08

Re: How to get digits after decimal comma?

Posted 07 February 2009 - 08:56 AM

:: here is a function that will do the work !

float getDecimalPart(floar number){
  int I = (int)number;
  number -= (float) I;
  return number;
}


Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: How to get digits after decimal comma?

Posted 07 February 2009 - 10:42 AM

View Postkrisku, on 7 Feb, 2009 - 05:53 AM, said:

I want to count sum of digits AFTER decimal comma, e.x.:
152.25 ==> 1-2-5 (haven't got problem for calc them), but i don't know how to calculate 2-5.


Do you mean after the decimal point? It should still have been enough to get the idea. :P
n = 152.25
t = floor(n) = 152
d = (n - t) = (152.25 - 152) = 0.25
n = (d*10) = (0.25*10) = 2.5



If you can't figure it out with floor and a base 10, then I'm afraid you can't do it.
Was This Post Helpful? 1
  • +
  • -

#9 krisku  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 65
  • Joined: 13-December 08

Re: How to get digits after decimal comma?

Posted 07 February 2009 - 04:39 PM

Thanks, I undarstand it but don't know how to make program to stop when haven't a decimal point. I try to do something with while, but haven't got idea again... ;-(

	while()  { // how will looks this?
 		d*=10;
		i++;
	}


This post has been edited by krisku: 07 February 2009 - 04:39 PM

Was This Post Helpful? 0
  • +
  • -

#10 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 275
  • View blog
  • Posts: 1,764
  • Joined: 20-September 08

Re: How to get digits after decimal comma?

Posted 08 February 2009 - 04:05 AM

Since floating point numbers are stored to only an approximate value in most cases ... your question would make more sense if you want to extract the decimal part ... and the integer part ... if your numbers are in a certain narrow range ... for example with absolute values in the range 0.000001 to 999999.0

Remember in 32 bit systems, floats often have about 6-7 sig. digits and doubles have about 15 or so? sig. digits max.

When the decimal value is converted to the approximate binary representation ... it really makes no sense asking how many decimal places will it have for any/all conversions ... The decimal places are fixed by the size of the memory assigned to hold the bits ... 16 bits, 32bits, 64bits, 80 bits ...etc ...

Shalom,
David

P.S.

If you use string input first, you might better handle getting the pre-decimal part and the post decimal part of the string input.
Was This Post Helpful? 0
  • +
  • -

#11 Plus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 41
  • View blog
  • Posts: 414
  • Joined: 24-November 08

Re: How to get digits after decimal comma?

Posted 08 February 2009 - 01:22 PM

:: maybe i wasn't very good,

template<typename t>
t GetDecimalPart(t number){
   int Integer = (int) number;
   t temp = number - (t)Integer;
   return temp;
}



:: and if you want them to be separated in an int array then use this one,

template<typename t>
int* GetDecimalPart(t number){
   int Array[256]={0};
   int Integer = (int) number;
   int size;
   t D = number - (t)Integer;
   for(size=0;D!=0.0;size++){
	   D *= 10;
	   Array[i] = (int)D;
	   D -=Array[i];
   }
   int *FinalArray = new int[size];
   for(int i=0;int i<size;i++)
	   FinalArray[i]=Array[size-(i+1)];
   return FinalArray
}



example:

  double A = 0.123456;

  int *B = GetDicmalPart<double>(A);


This post has been edited by Plus: 08 February 2009 - 01:41 PM

Was This Post Helpful? 0
  • +
  • -

#12 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 275
  • View blog
  • Posts: 1,764
  • Joined: 20-September 08

Re: How to get digits after decimal comma?

Posted 08 February 2009 - 10:58 PM

String input has many pluses ...

Quote

P.S.

If you use string input first, you might better handle getting the pre-decimal part and the post decimal part of the string input.


... simple, easy and crash proof too.

string numStr, beforeD, afterD = "0";
cin >> numStr;
cin.sync(); // flush cin stream ...

size_t found = numStr.find(".");
if(found != string::npos)
{
    cout << "The decimal '.' was found at index " << found << endl;
    beforeD = numStr.substr( 0, found );
    if( found < numStr.length()-1 ) afterD = numStr.substr(found+1);
}
else beforeD = numStr;




You may like to see this following example ...

Shalom,
David


#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

struct RealNum
{
    string iPart;
    string dPart;
};

void showReal( RealNum &n )
{
    std::cout << n.iPart << '.' << n.dPart << std::flush;
}

bool isNumber( const string &n )
{
    for( size_t i = 0; i < n.length(); ++i )
        if( n[i] < '0' || n[i] > '9' ) return false;
    return true;
}

// abs value range is 0.000000001 .. 999999999
void getReal( RealNum &n )
{
    string numStr, beforeD, afterD = "0";
    cin >> numStr;
    cin.sync(); // flush cin stream ...

    size_t found = numStr.find(".");
    if(found != string::npos)
    {
        cout << "The decimal '.' was found at index " << found << endl;
        beforeD = numStr.substr( 0, found );
        if( found < numStr.length()-1 ) afterD = numStr.substr(found+1);
    }
    else beforeD = numStr;

    // now validate ...
    bool ok = true;
    
    if( beforeD[0]== '-' )
    {
        if( beforeD.length() > 10 ) ok = false;
        else if( !isNumber( beforeD.substr(1) ) )ok = false;
    }
    else
    {
        if( beforeD.length() > 9 ) ok = false;
        else if( !isNumber(beforeD) ) ok = false;
    }

    if( afterD.length() > 9 ) ok = false;
    else if( !isNumber(afterD) ) ok = false;

    if( ok ) { n.iPart = beforeD; n.dPart = afterD; }
    else { n.iPart = "0"; n.dPart = "0"; cout << "\nENTRY ERROR ... " << flush; }
}

int main()
{
    for(;;)/>
    {
        cout << "Enter a 'real number' with some decimals (123.4567) : ";
        RealNum rn;
        getReal( rn );
        cout << "The real number is ";
        showReal( rn );
        cout << "\n\nMore ... (0 to quit) " << flush;
        string reply;
        getline( cin, reply );
        if( reply == "0" ) return 0;
    }
}



You could make this into a Class and write methods for adding, subtracting, multiplying, dividing, etc ... with these numbers.

Using strings, you could have as many digits as you like.

This post has been edited by David W: 08 February 2009 - 11:03 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1