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));
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
Convert string to double without scientific notation
The methods you have used do not return 1.08595E8
, instead, they return the number and what you are complaining about is the representation of that number in the console (or as a String
).
However, you can specify how to output a double
yourself with a specified formatting, see this example:
public static void main(String[] args) {
String value = "108595000.5";
// use a BigDecimal to parse the value
BigDecimal bd = new BigDecimal(value);
// choose your desired output:
// either the String representation of a double (undesired)
System.out.println("double:\t\t\t\t\t" + bd.doubleValue());
// or an engineering String
System.out.println("engineering:\t\t\t\t" + bd.toEngineeringString());
// or a plain String (might look equal to the engineering String)
System.out.println("plain:\t\t\t\t\t" + bd.toPlainString());
// or you specify an amount of decimals plus a rounding mode yourself
System.out.println("rounded with fix decimal places:\t"
+ bd.setScale(2, BigDecimal.ROUND_HALF_UP));
}
double: 1.085950005E8
engineering: 108595000.5
plain: 108595000.5
rounded with fix decimal places: 108595000.50
How to avoid scientific notation when converting small double values to string?
If you don't have a numeric source for that data and you actually need to parse the content of UI elements, the Culture used when inserting data matters, because not all cultures use a comma as decimal separator: if the current UI Culture (Thread.CurrentThread.CurrentCulture and Thread.CurrentThread.CurrentUICulture) don't match the input format (you show input that uses a comma instead of a dot - as the InvariantCulture - to separate the decimal part), then the text will not parse correctly or at all.
If you have a text input that uses a specific culture format, you need to parse that input specifying the corresponding CultureInfo.
Numbers don't have a format: if you have a numeric source, use that for the calculations, then present the data using the destination UI Culture to provide a localized representation of those value.
If the input Culture and the current Culture are the same, then you don't need to specify a CultureInfo when parsing string values, since the Culture returned by Thread.CurrentThread.CurrentCulture
is used.
Assuming that the input format is based on the Italian format for decimal numbers (assumption based on the use of the name PREZZO
, which Google Translate detects as Italian), you can create a CultureInfo that provides standard formats used in that culture.
When parsing the string values, pass this CultureInfo to the methods, so the text will be parsed correctly.
Also, since you dealing with currency, don't use Double or CDbl
to parse those values, use Decimal.Parse() instead. For example:
Dim PREZZO = "0,04831"
Dim currentPrice = Label16.Text ' "0,04840"
Dim culture = CultureInfo.CreateSpecificCulture("it-IT")
Dim price = Decimal.Parse(PREZZO, culture)
Dim price1Year = Decimal.Parse(currentPrice, culture)
Dim priceDiff = price1Year - price
Dim priceDiffPercent = priceDiff / price
Now, to present the calculated variation in price and the percentage of the variation, you need to format back those values using the same CultureInfo:
labelDiffPrice.Text = priceDiff.ToString("N5", culture)
labelDiffCurrency.Text = priceDiff.ToString("C5", culture)
labelDiffPercent.Text = priceDiffPercent.ToString("P5", culture)
N5
specifies a number with a precision of 5 decimal values.C5
species to use Currency format and Symbol defined by the CultureInfo, with a precision of 5 decimal values. This overrides the CultureInfo.NumberFormat.CurrencyDecimalDigits, so it should be used for a specific purpose, as in this case.P5
a percentage representation of a number multiplied by 100
with a precision of 5 decimal positions.
The two calculated values will be presented as:
' Variation in price
0,00009
' Variation in price expressed in currency
€ 0,00009
' Percentage of the variation
0,18630%
If the input comes directly from a User, use Decimal.TryParse() instead of Decimal.Parse()
to validate the input.
See also: Standard numeric format strings
Related Topics
Arrayindexoutofboundsexception with Custom Android Adapter for Multiple Views in Listview
Recyclerview with Multiple Countdown Timers Causes Flickering
Clearing Notification After a Few Seconds
Broadcast Receiver for Missed Call in Android
Android Webview Hardware Rendering Weird Artifact Issue
How to Open One Particular Folder from Gallery in Android
Android Device Configuration for Aosp
Lucky Patcher, How to Protect from It
Android Webview Jellybean -> Should Not Happen: No Rect-Based-Test Nodes Found
How to Download Xml File from Server and Save It in Sd Card
Is There a Faster Way to Decode HTML Characters to a String Than HTML.Fromhtml()
Captured Photo Orientation Is Changing in Android
How to Add a Bullet Symbol in Textview
Supportmapfragment.Getmap() Returns Null
Expandablelistview with Viewpager Combination as Its Child
How to Add Apache Commons Collections in Android Studio (Gradle)