2

I have a problem with casting; I have experience from Java where it is easy to cast and print:

int a = 1;
System.out.println((double)a);

and that code above will print 1.0.

In C++ this type of method don't work and I don't understand why.

#include <iostream>
using namespace std;
int main(){
    int a = 1;
    cout << (double)a;
    return 0;
}

The problem is, here my output is 1 and not 1.0 like I would expect.

Lex
  • 6,758
  • 2
  • 27
  • 42
CivilEngine
  • 47
  • 1
  • 5
  • 6
    I believe most implementations hide trailing zeroes. If you want a specific amount of digits theres some functions for that, just do a quick google search for it specifically. – Borgleader Jul 29 '16 at 13:46
  • this is more c than c++ answer, but try with printf("%.1f", (double)a); – FrankS101 Jul 29 '16 at 13:52
  • @FrankS101 : An I heard somebody say don't c++, the c way ;) – sjsam Jul 29 '16 at 14:17
  • Get out of the habit of using C-style (and Java-style I guess) casts and use proper C++style casts instead (if you really *have* to use a cast that is - usually best with a design where you don't need them at all). C-style casts are actively dangerous since it can perform many different casts and often some you don't really want - they are also hard to search for in code - searching for, for example, `static_cast` is much easier. – Jesper Juhl Jul 29 '16 at 14:34
  • 1
    *can't understand why.* -- The reason is that Java is not C++. Don't use Java as a model in writing C++ code. Yes, there is similar syntax between the language, but don't be fooled into thinking you can take what you see in Java, and because there is something similar in C++ that looks like Java, expect to see the same results. – PaulMcKenzie Jul 29 '16 at 14:36

5 Answers5

4

After setting cout to use fixed notation, you can use setprecision function to set precision of cout object.

#include <iostream>
#include <iomanip> 
using namespace std;

int main(){
    int a = 1;
    cout << fixed            // setting notation
         << setprecision(1)  // setting precision
         << (double)a;       // print data
    return 0;
}

Output (stdout):

1.0
frogatto
  • 28,539
  • 11
  • 83
  • 129
1

You can specify precision and fixed for cout output for floating point types. Put:

cout << fixed << setprecision(2);

before printing your doubles.

Fully working example:

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
   int a = 1;
   cout << fixed << setprecision(2);
   cout << (double)a << endl;
   return 0;
}
vordhosbn
  • 333
  • 4
  • 16
1

To me it makes no sense to convert int to double because the fractional data will be lost once you write it against int. Moreover as stated by @Borgleader I guess the trailing zeroes are hidden in C++.

However you can convert a float or double to int or an int to char or vice versa using the same method which you showed in your code.

  • In case of float or double to int , the fractional data will be lost
    i.e. 1.234 will become 1.
  • In case of char to int the resulting value will be the ASCII value of that character
    i.e. A will become 65 , 0 become 48 and in a vice versa situation 97 will become a.

Hope this answer helped you.

Itban Saeed
  • 1,660
  • 5
  • 25
  • 38
0

If you explicitly do not specify the floating point formatting mode then trailing zeros are not kept. It will be kept if you use the scientific mode or fixed mode. You can try to get the zeros like

 int a = 1;

  std::cout << std::fixed;
  std::cout << std::setprecision(1);
  std::cout << (double)a << "\n";

IDEONE DEMO

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
-1

You can convert it into a string if you are using C++11 or greater

std::string str = std::to_string (double(f));

Unfortunately this doesn't let you control the formatting, and will produce unnecessary trailing zeros. You can get rid of these by doing this:

str.erase (str.find_last_not_of('0') + 1, std::string::npos);

The one issue is that if you have "1.0" for example, it will produce "1."

Dillydill123
  • 693
  • 7
  • 22
  • 4
    Not the downvoter: but there's something called *efficiency*, you don't need to convert to a string manually to set precisions of floating point numbers in C++ streams.... again, this solution smells – WhiZTiM Jul 29 '16 at 14:00
  • Fair enough, I was just providing another way of doing it. – Dillydill123 Jul 29 '16 at 14:03