hello i am having a little problem, I cant convert a double to one decimal place:
here is my code

for(int i=0; i<marklist.size();i++)
  {
double result=floor((marklist.at(i) /maxmark));

  cout<< result<<endl;
  }

I want result to be one decimal place

I cant use set precision because it only works for std output

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    double x = 800000.0/81.0;
    cout << setiosflags(ios::fixed) << setprecision(1) << x;
    return 0;
}
 this is answer
 cout << setiosflags(ios::fixed) << setprecision(1)<< result<<endl;

Edited 3 Years Ago by mike_2000_17: Fixed formatting

You mean 1 decimal place after the point? Like 13.4 or 2333.4. I'll go with yes.

If you want to obtain the value with 1 decimal place after the point, then you should do this
double x = some value;
double y = x = int(x*10)/10.0;
this will however not round the number, that is,
if x were 14.39 y would be 14.3, not 14.4
if you want to round it, then you should do this
y = int((x+0.05)*10)/10.0
in this case if x is 14.39 then y will be 14.4

If you do not need the value, but you just need to stream/output the value with 1 decimal place, it's easier, that is

#include <iomanip>

cout << setprecision(1) << x;

but what if I want to make result equal to two decimal places before outputting it

in this case

cout << setprecision(2) << x << endl;

or, if you want to OBTAIN the value, then
y = int(x*100)/100.0 //without rounding
or,
y = int((x+0.005)*100)/100.0

:) Hope you see the pattern

This article has been dead for over six months. Start a new discussion instead.