How to nicely format floating numbers to string without unnecessary decimal 0’s

If the idea is to print integers stored as doubles as if they are integers, and otherwise print the doubles with the minimum necessary precision: public static String fmt(double d) { if(d == (long) d) return String.format(“%d”,(long)d); else return String.format(“%s”,d); } Produces: 232 0.18 1237875192 4.58 0 1.2345 And does not rely on string manipulation.

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

Java prevent E notation in a double: Five different ways to convert a double to a normal number: import java.math.BigDecimal; import java.text.DecimalFormat; public class Runner { public static void main(String[] args) { double myvalue = 0.00000021d; //Option 1 Print bare double. System.out.println(myvalue); //Option2, use decimalFormat. DecimalFormat df = new DecimalFormat(“#”); df.setMaximumFractionDigits(8); System.out.println(df.format(myvalue)); //Option 3, use … Read more

Round a double to 2 decimal places [duplicate]

Here’s an utility that rounds (instead of truncating) a double to specified number of decimal places. For example: round(200.3456, 2); // returns 200.35 Original version; watch out with this public static double round(double value, int places) { if (places < 0) throw new IllegalArgumentException(); long factor = (long) Math.pow(10, places); value = value * factor; … Read more

Convert double * to double [9] in C [closed]

#include <stdio.h> #include <string.h> typedef struct dbl9 { double array[9]; } Dbl9; Dbl9 fun(double *in){ Dbl9 ret; memcpy(ret.array, in, sizeof(ret.array));//There can be no assurance that <in> the correct return ret; } int main(){ double array[9] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0}; Dbl9 dbl9= fun(array); int i; for(i=0;i<9;++i){ printf(“%f\n”, dbl9.array[i]); } return … Read more