I runned my arithmetic operations with double data type and i got o/p for postive,negative and decimals nos and if i am using three pont decimal no(0.0005) means i am getting as (2.5E-7 & 4.0E-4) two diff nos and this is my code

import java.io.*;
public class arithmetic 
{
	public static void main(String args[]) throws IOException
	{
	double add;
	double sub;
	double multiply;
	double divide;
	BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
	System.out.print("Enter the Value of a : ");
	double a = Double.parseDouble(br1.readLine());
	System.out.print("Enter the Value of b : ");
	double b = Double.parseDouble(br1.readLine());
	add = a + b;
	sub = a- b;
	multiply = a*b;
	divide = a/b;
	System.out.print("After Airthmetic operations:\n");
	System.out.println("Result of Addition: "+add);
	System.out.println("Result of Subraction: "+sub);
	System.out.println("Result of Multiplication: "+multiply);
	System.out.println("Result of Division: "+divide);
}
}

Dani AI

Generated

Short answer: Java is showing those tiny results in scientific (E) notation because of the way floating‑point values are converted to text. Double.toString will choose exponential form when the decimal exponent is less than -3 or greater than or equal to 7, so very small numbers are printed like 4.0E-4 rather than 0.0004. ’s point that this is a display choice (not a different value) is correct. (docs.oracle.com)

If you only want nicer output, format the printed value. The Formatter/printf conversions let you force fixed or scientific output (%f = fixed, %e = scientific, %g = adaptive), and DecimalFormat gives you pattern control and localization. Example options:

DecimalFormat df = new DecimalFormat("#.######");
System.out.println("Result: " + df.format(value));

System.out.printf("Division: %.6f%n", divide);

Use the approach that matches how many digits you want shown. (docs.oracle.com)

If correctness (not just appearance) matters — for example currency, exact decimal input, or reproducible rounding — use BigDecimal instead of double. Create BigDecimal from the original input string (or use BigDecimal.valueOf when converting from a double) and convert to plain text with toPlainString() to avoid scientific notation. Also use setScale(..., RoundingMode) for precise rounding. Avoid new BigDecimal(double) because it captures the binary approximation, which produces unexpected long decimals. (docs.oracle.com)

Practical checklist: display-only → printf or DecimalFormat; accurate decimal math → BigDecimal from String/valueOf + toPlainString() + setScale(...); don’t rely on double for exact decimal tasks. This keeps both appearance and arithmetic correct for tiny values.

Recommended Answers

All 9 Replies

bit confused here. what is your actual question?

i was asked to do airthmetic operations for all possible numerics where i got the o/p correctly for positive, negative and some decimal numbers and if i am trying for very small decimal nos like 0.0005 etc i am getting the outputs like i showed above in the bracket.

you are getting exponent representation cause the results are really large /small so

2.5E-7 = 2.5 * 10^-7 = 0.00000025
4.0E-4 = 4 * 10^-4 = 0.0004

using

System.out.printf

to format your output

Thanks a lot ejosiah
And one thing where should i use this "System.out.printf" or i should use instead of "println"

replace your last 4 print statements with printf. go through this site System.out.printf.htm should help you understand how to use printf

Thnks and i did as you said and i am getting the same o/p in a single line nothing is changed :):):)

like this.

System.out.printf("Result of Multiplication: %.9f\n" , multiply);

thnks a lot ejosiah the prob is solved now and ur the best:):):):)

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.