How to round Double to significant digit [duplicate] - java

This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 8 years ago.
I have a application which reads SAS xpt file and stores column value into ByteBuffer and then using getValue() method of it to get Double object. Now I have to print Double upto 12 significant digit after decimal. I found one answer from this from which is working fine but few cases
BigDecimal bd = new BigDecimal(dblColumnData.doubleValue());
System.out.println(String.format("%."+15+"G", bd));
Here 15 is given because there are 3 digits in integer part and there must be 12 significant digit after decimal.
Cases where it is not working is because BigDecimal created from Double. If I print Double then it contains more than 12 digits after decimal and it round correctly with same above approach.
Therefore I think if I can get similar format method for Double the it will solve my problem.

I'd do it somehow like this:
static double roundTo(double d, int digit) {
double exp = Math.pow(10, digit);
d *= exp;
d = Math.round(d);
return d / exp;
}
The following would round the number to 3 places after the comma and print it
public static void main(String[] args) {
System.out.println(roundTo(7.34343434, 3));
}
Will print "7.343"

Related

How to convert from a double with or without a fractional part to a hexaicosadecimal string (Base-26)? [duplicate]

This question already has an answer here:
How to convert from a double without a fractional part to a hexaicosadecimal string (base-26)?
(1 answer)
Closed 1 year ago.
What is the way to convert from a double with or without a fractional part to a hexaicosadecimal string? What are some tips or possible shortcuts?
For any base B, multiply the factional decimal number part by B.
If the number is >= 1, assign to an int and convert to 0-F as appropriate.
save only the previous fraction and continue from the beginning.
Here is an example for binary.
double d = .2342;
System.out.print(".");
for (int i = 0; i < 20; i++) {
d = 2*d;
int bit = (int)d;
System.out.print(bit);
d = d-bit;
}
System.out.println();
Prints the following for .2342 with 20 bits of accuracy
.00111011111101001000
Note that since floating point products of fractions can degrade, care must be taken for long strings. And unless the fraction is a power of.5, it will be repeating.

Take the decimal part of a double/float number [duplicate]

This question already has answers here:
How do I get whole and fractional parts from double in JSP/Java?
(18 answers)
Closed 4 years ago.
I have a simple question, I want to know how can I get the decimal part from a double/float number without the dot.
Example: a=0.75 and b=3231.0131
So I would like to set those decimal values in two new Integer variables: m=75 and b=0131.
I'm going to clarify some things, I want to create a new int variable, that variable will storage the decimal part from the original number.
double a = 0.75
double b = 12.033
int x = decimalofa
int y = decimalofb
System.out.println("the decimal of"+a+"is"+x+"and the decimal of"+b+"is"+y)
//the decimal of 0.75 is 75 and the decimal of 12.033 is 033
The thing is that i'm not sure if 033 could be considered as an integer number, so in other words I just want to take all the numbers next to the point and save them in a new variable.
Just do
float a = 0.75f;
System.out.println(Float.toString(a).split("[.]")[1]);
This only works if there is a decimal and there are numbers after that decimal

How to modify Double value in android [duplicate]

This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Double decimal formatting in Java
(14 answers)
Closed 6 years ago.
Suppose, I have a double value.
double dble = 5.91742691
Now can I get that value with two digits after the point. I mean to say,can I get 5.91 from there programmatically?
One more, suppose, I want to get an integer from a double value if the double value is X.9XXXXX. Here I mean to say, I want to compare the AFTER-POINT value. For your understanding here dble's AFTER-POINT value is 91742691. How can I do that?
If you want to truncate a double to 2 decimal places, do
double twoDecDbl = (int)(dble * 100) / 100.0; // 5.91
or
double twoDecDbl = Math.floor(dble * 100) / 100; // 5.91
However, if you want to get the numbers after the decimal place as an integer (which I don't know why you would want to do this), then do
long decimals = Long.parseLong(("" + dble).split("\\.")[1]); // 91742691
Note: The maximum value of an int is 2,147,483,647 (10 digits there), but a double can hold 16 digits after the decimal point, so a long must be used in order to stay safe (can have 19 digits). Alternatively, you can just keep it as a String by removing the wrapping parse.
For #1 Try it
NumberFormat formatter = new DecimalFormat("#0.00");
String s = formatter.format(dble);
double num = Double.valueOf(s);
or
dble = Math.floor(dble * 100) / 100;
For #2 Try it
double num = dble - (int)dble;
In case Integer.MIN_VALUE < dble < Integer.MAX_VALUE

Java how to divide 2 doubles with 2 digits precision? [duplicate]

This question already has answers here:
How can I truncate a double to only two decimal places in Java?
(18 answers)
Closed 8 years ago.
amount / 100 * 7 - I'am trying to get a percent from an amount, but the problem is that sometimes a get a number with to many digits after dot, how can I make it strictly return 2 digits after dot?
type is double
Use DecimalFormat API
double d = amount / 100 * 7;
DecimalFormat df = new DecimalFormat("#.##");
System.out.print(df.format(d));
"##" denotes the 2 digit precision
You can do this
double d = Math.round(amount * 7) / 100.0;
This will give you have value which has two decimal places. (for a modest range of values i.e. < 70e12)
If you just want to print two decimal places you can use
System.out.printf("%.2f%n", d);

How to truncate the double value? [duplicate]

This question already has answers here:
Are there any functions for truncating a double in java?
(11 answers)
Closed 9 years ago.
I want to truncate the double.
e.g.
double d=3.123456789087654;
if I want to truncate It to the 10th digit after the decimal
the result should be
result: 3.1234567890
I don't need round off value as a result
I tried my own function as
static double truncateDouble(double number, int numDigits) {
double result = number;
String arg = "" + number;
int idx = arg.indexOf('.');
if (idx!=-1) {
if (arg.length() > idx+numDigits) {
arg = arg.substring(0,idx+numDigits+1);
result = Double.parseDouble(arg);
}
}
return result ;
}
but didn't get the appropriate result as I want..
can anybody please help me to clear my problem.
Does java supports such any function that supports truncation not round off??
In general, you can't, at least not by using double. The reason is that many numbers simply can't be represented exactly, even if they have a small number of decimal digits.
Take 0.1 as an example. The nearest double is 0.1000000000000000055511151... and no amount of truncation would give you exactly 0.1.
You can use the DecimalFormat class.
double d = 3.123456789087654;
DecimalFormat newFormat = new DecimalFormat("#.##########");
double tenDecimal = Double.valueOf(newFormat.format(d));
this will round the last digit.
I am agree with NPE. no amount of truncation would give you exactly 0.1.
only way is to convert in to string and after substring convert back to double.

Categories

Resources