# I would like to see a value of a decimal without C ++ being rounded

3

I need help with a code in C ++, the exercise asks me to do a calculation, if the result is, for example 1.57, in the output it should show 1.5, (the variable can be float or double) without making the rounding.

``````#include <iostream>
#include <stdio.h>
using namespace std;

int main()
{
int c=0;
cin >> c;
while(c--){
int Vkh=0;
double Vms=0, Vm=0;
cin >> Vkh;
Vms=Vkh/3.6;
Vm=Vms/340;
printf(" %.1f\n", Vm);
}
return 0;
}
``````

asked by ENez7 07.08.2017 в 19:31
source

3

The `cmath` library ( `math.h` for those coming from C), has a `trunc` function that allows you to truncate a `float` or a `double` . The truncation function eliminates the decimal part of the number. To eliminate a certain number of decimals you can choose to temporarily include those decimals in the whole part of the number, apply truncated and reposition them as decimals:

``````#include <cmath>
#include <iomanip>
#include <iostream>

int main()
{
float numero = 1.57;
std::cout << std::fixed << std::setprecision(1) << std::trunc(numero*10)/10;
}
``````

Example in wandbox

This can be left more nice if we divide the code into functions:

``````constexpr double Exp(int numero)
{
return ( numero <= 0 )? 1 : 10 * Exp(numero-1);
}

double Truncar(double numero, int decimales)
{
return std::trunc(numero*Exp(decimales))/Exp(decimales);
}

void ImprimirDouble(double numero, int decimales)
{
std::cout << std::fixed << std::setprecision(decimales) << Truncar(numero,decimales) << '\n';
}

int main()
{
double numero = 3.777;
ImprimirDouble(numero,0);
ImprimirDouble(numero,1);
ImprimirDouble(numero,2);
}
``````

Example in wandbox

Although I personally like more the option of giving a strong typing to the variable ... the code is cleaner. The example can be abstracted with templates and so on, but I think that it would lose the focus of what I'm trying to teach:

``````namespace MathUtils
{

constexpr double Exp(int numero)
{
return ( numero <= 0 )? 1 : 10 * Exp(numero-1);
}

double Truncar(double numero, int decimales)
{
return std::trunc(numero*Exp(decimales))/Exp(decimales);
}

}

class Double
{
double value;

public:

explicit Double(double value)
: value(value)
{ }

operator double() const
{ return value; }

friend std::ostream& operator<<(std::ostream& os, Double const& value);
};

std::ostream& operator<<(std::ostream& os, Double const& value)
{
auto precision = os.precision();
return os << std::fixed << MathUtils::Truncar(value,precision);
}

int main()
{
Double numero(3.777);
std::cout << std::setprecision(0) << numero << '\n';
std::cout << std::setprecision(1) << numero << '\n';
std::cout << std::setprecision(2) << numero << '\n';
}
``````

Example in wandbox

• The output to the console or file is more natural.
• Having a strong typing prevents unwanted conversions.
• The code is much more expressive. It may not be appreciated in this example but on a larger scale it is seen quickly.
source
3

This is a simple and fast way, I'm not proud of it, there must be a better one ...: - (

``````#include <cstdio>
using namespace std;

int main() {
printf("%f\n", 3.0/7);
printf("%.4f\n", 3.0/7);
printf("%.4f\n", (float)((int)(3.0/7*10000))/10000);
return 0;
}
``````

### Exit

``````\$ g++ stack.cpp && ./a.out
0.428571
0.4286
0.4285
``````