Printing a double without a decimal point?

I want to print out the result of my output without a decimal. Essentially, like an int. I was looking into bit shifting, but ran into a dead end. It seems so simple, yet I've struggled for three days.

1
2
3
4
5
6
7
8
9
10
int main()
{
  double num1 = 2; 
  double num2 = 3.1415926535897932384626433832;

  std::cout << setprecisision(100) << log(num1) << endl;
  std::cout << setprecisision(100) << log(num2) << endl;
  
  return 0;
}


Actual Output:
0.69314718055994528622676398299518041312694549560546875
1.1447298858494001638774761886452324688434600830078125

Output I want:
69314718055994528622676398299518041312694549560546875
11447298858494001638774761886452324688434600830078125
Last edited on
std::cout << std::fixed << std::setprecision(100) << number << std::endl;

I like that you are using std:: in front of cout, but it really needs to be used everywhere if you use it in even one place. Otherwise I would say not to use it at all. (I recommend using it everywhere :)

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

And welcome to the forums! Please be sure to use code tags in the future. Simply click the <> button to the right of an edit text box.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>

std::string myformat(double val)
{
    std::ostringstream os;

    os << std::setprecision(100) << val;

    bool dot = false;
    bool leading_zero = true;

    std::string result;
    for (char ch : os.str()) {
        if (leading_zero && ch == '0') continue;

        switch (dot) {
        case false:
            if (ch == '.') {
                dot = true;
                break;
            }
 
            // fall through
        case true:
            leading_zero = false;
            result += ch;
            break;
        }
    }
    return result;
}

int main()
{
    double num1 = 2;
    double num2 = 3.1415926535897932384626433832;

    std::cout << std::setprecision(100) << myformat(log(num1)) << std::endl;
    std::cout << std::setprecision(100) << myformat(log(num2)) << std::endl;

    return 0;
}


I should note this doesn't handle negative numbers with leading zeros well. You'll have to adjust the code if you want to handle that.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <cmath>
#include <iomanip>
#include <sstream>
#include <string>

std::string digits( double value, unsigned int n )
{
    if( std::isnan(value) ) return "nan" ;
    if( std::isinf(value) ) return "inf" ;

    std::ostringstream stm ;
    stm << std::fixed << std::setprecision(n*2) << value ;
    std::string str = stm.str() ; // get the string representation

    if( str.front() == '-' ) str.erase( 0, 1 ) ; // remove leading - sign if present

    const auto pt = str.find( '.' ) ;
    if( pt != std::string::npos ) str.erase( pt, 1 ) ; // remove decimal point if present

    auto first = str.find_first_not_of( '0' ) ; // find first non-zero digit
    // adjust if less than n digits after first non-zero digit
    if( first == std::string::npos || first > n ) first = n ;

    str = str.substr(first,n) ; // truncate to n digits after first
    while( str.size() > 1 && str.back() == '0' ) str.pop_back() ; // remove trailing zeroes
    return str ;
}

int main()
{
    for( double num : { 2.0, 3.1415926535897932384626433832, 1.0e-100, 1.0e+100 } )
        std::cout << digits( std::log(num), 100 ) << '\n' ;
}

http://coliru.stacked-crooked.com/a/c88e1d305f86ae7a
Last edited on
closed account (48T7M4Gy)
http://www.cplusplus.com/reference/ios/noshowpoint/
The OP might want it? After all 0.6931471<snip> was to be displayed without the "0." as 6931471<snip>
Yes. I suppose the OP might be happy with having a single edge case handled. ;)
closed account (48T7M4Gy)
Not useless. Take your Ritalin Cire
Utterly useless for the OP's purpose.

noshowpoint does not do what you fondly believe it does.
noshowpoint does not do what you fondly believe it does.

I did make an assumption (based on the name of the function) that the decimal point would not be shown for numbers less than 0. If that is not the case, could the noshowpoint page be edited to show an example of a number less than 0?
1
2
3
4
5
6
7
#include <iostream>

int main()
{
    for( double v : { 1.0, 1.01, -1.0, -1.01, 0.0, 1.0e+10, -1.0001e-10 } ) 
        std::cout << "showpoint: " << std::showpoint << v << "  noshowpoint: " << std::noshowpoint << v << '\n' ;
}

showpoint: 1.00000  noshowpoint: 1
showpoint: 1.01000  noshowpoint: 1.01
showpoint: -1.00000  noshowpoint: -1
showpoint: -1.01000  noshowpoint: -1.01
showpoint: 0.00000  noshowpoint: 0
showpoint: 1.00000e+10  noshowpoint: 1e+10
showpoint: -1.00010e-10  noshowpoint: -1.0001e-10

http://coliru.stacked-crooked.com/a/d733185a14dc5197
Last edited on
So what I see is that it really only truncates any zeros to the right of decimal, and if no other number remains to the right of the decimal, the decimal is also clipped. If a number is less than zero, the "0." still remains on the left of the number. My assumpiton was incorrect.

So yeah, it seems useless for the OP. Time to go take my Ritalin.
Last edited on
closed account (48T7M4Gy)
It's always good to get oporto-girl working for us and tidy up, saves so much time on boring detail.
My gratitude to the solutions given. They were excellent and make me look into stringstream functionality more. Ostensibly I'm new at this and will get my coding input up to par next time I post something.
Cire I'm using your version of this. It's excellent. I also want it to print 0 for log(1), but I want to figure that one out for myself as I need to learn this.
Topic archived. No new replies allowed.