Extract fractional part

Hi
how could I extract fractional part of a float/double with 11 decimal?
I've got some codes here but just extract 6 decimal.
Please help me
Most likely all the decimal places are there, you just don't see them in the output.
#include <iomanip>

std::cout << std::fixed << std::setprecision(11);

http://www.cplusplus.com/reference/iomanip/setprecision/
http://www.cplusplus.com/reference/ios/fixed/

Also, for getting the fractional part, see
http://www.cplusplus.com/reference/cmath/modf/
Last edited on
Thank you so so much.
How could I have the fraction pat without "0." ? just the number.
thanks
this the code I used :

double w = 7.00123456789;
cout << "the main number is : "<<w<<'\n';
int week = w; // It will contain only 7
//You can get the fractional part:
cout << "the Int part is : " <<week<<'\n';
double tmp_d = w - week; // it will be 0.decimal part
cout << setprecision(11)<<"the Decimal part is : " <<tmp_d<<'\n';

the result is :

the main number is : 7.00123456789
the Int part is : 7
the Decimal part is : 0.00123456789

I just need 00123456789
Last edited on
Either turn it into a string. Or maybe multiply the number by 1.0e11
but I could not turn it to a string properly including all the 11 digits.
and could not use th ToString function at all, I don't know why.
I am using Microsoft Visual Studio 2010.

when I use multiply the number by 1.0e11 it change the number on 11 digits.
0.00123456789*1.0e11 = 123456789
but :
long int multiply = 0.12345678999* 1.0e11; = -2147483648
float multiply = 0.12345678999* 1.0e11; = 12345678848.00000000000
double multiply = 0.12345678999* 1.0e11; = 12345678998.99996600000

what can I do ? help me please
thank you
1
2
3
4
5
6
7
unsigned long long ipow( unsigned int b, unsigned int p ) { return p == 0 ? 1 : b * ipow( b, p-1 ) ; }

long long fractional_part( double dbl, unsigned int ndigits = 11 )
{
    ndigits = std::min( std::numeric_limits<long long>::digits10 - 1, int(ndigits) ) ;
    return std::llabs( std::llrint( ( dbl - std::trunc(dbl) ) * ipow( 10, ndigits ) ) ) ;
}

http://coliru.stacked-crooked.com/a/ed2ab4b397832e2f
thanks it should be the best .
I tried to use this code but got an error ; Error: namespace "std" has no member "llrint" and "trunc" as well.
I've used the #include <cmath> and #include <math.h>
Ah, Microsoft Visual Studio 2010, is it?

Strongly consider moving ahead to the current version (2015).

With 2010, try this:
1
2
3
4
5
6
7
8
unsigned __int64 ipow( unsigned int b, unsigned int p ) { return p == 0 ? 1 : b * ipow( b, p-1 ) ; }

__int64 fractional_part( double dbl, unsigned int ndigits = 11 )
{
    ndigits = std::min( std::numeric_limits<__int64>::digits10 - 1, int(ndigits) ) ;
    if( dbl < 0 ) dbl = -dbl ;
    return __int64( ( dbl - std::floor(dbl) ) * ipow( 10, ndigits ) + 0.5 ) ;
}

http://rextester.com/KBZ3049
Thank you so so so so much for your help.
I used the multiply the number by 1.0e11 with "unsigned long long" argument and it worked.
thanks again, that was your kind of it.
but the last code you've sent was absolutely great.
Last edited on
Topic archived. No new replies allowed.