#include <iostream>

using namespace std;

int main()
{
	double var1 = 0;

	var1 = 2/10;

	cin.ignore();
	cin.get();

	return 0;
}

Its not setting var1 to .2 it is keeping it at 0. wth?
it does 10/2 = 5, but then it does 2/10 = 0.

Dani AI

Generated

A quick, practical follow-up that expands on what ran into and what and pointed out.

When both operands are integral types the division is performed using integer arithmetic: the fractional part is discarded during the division itself, then that integer result is converted to double when assigned. To get a fractional result, make sure at least one operand is floating point or explicitly cast one operand to a floating type. Prefer static_cast for clarity:

int num = 1;
int den = 2;
double result = static_cast<double>(num) / den;   // 0.5

Notes and troubleshooting tips:

  • Assigning the integer result to a double after integer division does not recover the lost fraction; the truncation already happened.
  • Use a floating literal (e.g., 1.0) or static_cast<double>(x) to force floating-point division.
  • Turn on compiler warnings to catch these cases early (GCC/Clang: -Wall -Wextra -Wconversion -std=c++17; MSVC: /W4).
  • Remember signed integer division truncates toward zero per the standard, which affects negative operands.

For the formal rules on how C++ chooses arithmetic types and performs conversions, see the cppreference discussion on arithmetic operators and usual arithmetic conversions: cppreference: arithmetic operators.

Apparently using decimal format is required. Never mind

Yup, your program was doing integer division, therefore 2/5 is 0 but 2.0/5 will give you the result you desire.

I know you said it's solved but just to clarify for you. Dividing 2 integers gives an integer result (0) which is then assigned to the double. You found the solution already.

Thanks for replys

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.