Format Double Value in Scientific Notation

How do I print a double value without scientific notation using Java?

You could use printf() with %f:

double dexp = 12345678;
System.out.printf("dexp: %f\n", dexp);

This will print dexp: 12345678.000000. If you don't want the fractional part, use

System.out.printf("dexp: %.0f\n", dexp);

0 in %.0f means 0 places in fractional part i.e no fractional part. If you want to print fractional part with desired number of decimal places then instead of 0 just provide the number like this %.8f. By default fractional part is printed up to 6 decimal places.

This uses the format specifier language explained in the documentation.

The default toString() format used in your original code is spelled out here.

How to Avoid Scientific Notation in Double?

Check answer here. You can write

moneyToGet.setText(String.format("%.0f", priceValue));

Fomat float and double into scientific notation in specific format given below

As you want the non zero digit of the number starts from just after the decimal.

you can do

    BigDecimal x = new BigDecimal("13.33");
DecimalFormat frmt = new DecimalFormat(".00E00");
String formatted = "0" + frmt.format(x.doubleValue());
System.out.println("result: " + formatted);

results

13.33
result: 0.13E02
0.00023
result: 0.23E-03

Formatting A Double In A String Without Scientific Notation

Use just %f instead of %.0f.

import java.math.BigDecimal;

public class Main {
public static void main(String[] args) {
double foo = 123456789.1234;
String str = String.format("%f", foo);
System.out.println(str);

// If you want to get rid of the trailing zeros
str = new BigDecimal(str).stripTrailingZeros().toString();
System.out.println(str);
}
}

Output:

123456789.123400
123456789.1234

Double value to scientific notation with fixed exponent C#

double value = 1427799000;
Console.WriteLine(value.ToString("G2", CultureInfo.InvariantCulture));

//output: 1.4E+09

The General ("G") Format Specifier

The general ("G") format specifier converts a number to the most
compact of either fixed-point or scientific notation, depending on the
type of the number and whether a precision specifier is present.

EDIT: About your comment you can't display the scientific notation in your desirable way, it is not defined that way ! The coeficient must be greater or equal to 1 and less to 10.

For number 1.23*10^11 ->Article source

The first number 1.23 is called the coefficient. It must be greater than or equal to 1 and less than 10.

The second number is called the base . It must always be 10 in
scientific notation. The base number 10 is always written in exponent
form. In the number 1.23 x 10^11 the number 11 is referred to as the
exponent or power of ten.

How to format a double so, that it will never show scientific notation?

A 5-minute search on Google brought me here:
How to print double value without scientific notation using Java?

This is what you want right?

Here's the same thing, but in a function:

public static String dNoScience(double d) {
return String.format("%.0f\n", d);
}


Related Topics



Leave a reply



Submit