Converting Byte to Megabyte in Java getting zero [duplicate] - java

This question already has answers here:
Why is Java's division broken?
(6 answers)
Closed 4 years ago.
I was having some problem when trying to convert bytes to megabyte in Java. What I have tried to do is:
long inodeSpace = 100000;
long MEGABYTE = 1024L * 1024L;
long inodeSpaceInMb = inodeSpace / MEGABYTE;
System.out.println("INODE SPACE " + inodeSpace);
System.out.println("INODE MB " + inodeSpaceInMb);
I tried to print out the inodeSpaceMb, however, I am getting 0. Any ideas why is it so?

Cast one of the two into a double and store the result in a double. Like:
double inodeSpaceInMb = (double) inodeSpace / MEGABYTE;

If you also want some precision to a specific decimal place when you do the conversion then you can do this:
public double bytesToMegabytes(long byteValue, int precision) {
if (precision < 0) {
throw new IllegalArgumentException("Precision can not be less than 0!");
}
double mbValue = byteValue * 0.000001;
BigDecimal bigDec = new BigDecimal(mbValue);
bigDec = bigDec.setScale(precision, RoundingMode.HALF_UP);
return bigDec.doubleValue();
}

Related

Java - My program is rounding down when it should be rounding up [duplicate]

This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
What is a debugger and how can it help me diagnose problems?
(2 answers)
Closed 4 years ago.
I am building a program that calculates the total cost of a restaurant bill. My program is rounding $18.135 down to $18.13. This is the code that I am using to round it to two decimal places:
tip = Math.round((charge * TIP_PERCENTAGE) * 100) / 100d;
All of the variables are doubles, and charge is equal to 100.75 and TIP_PERCENTAGE is equal to 0.18.
I tried a couple of things but I have not had any luck. I want the program to round $18.135 up to $18.14.
Try this
private static double round(double value, int places) {
if (places < 0) throw new IllegalArgumentException();
BigDecimal bd = new BigDecimal(Double.toString(value));
bd = bd.setScale(places, RoundingMode.HALF_UP);
return bd.doubleValue();
}
...
tip =(charge * TIP_PERCENTAGE) ;
tip = round(tip,2);

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

Round up when multiplying an integer by 0.5 [duplicate]

This question already has answers here:
Always Round UP a Double
(8 answers)
Closed 7 years ago.
Is there a way to automatically round up a value in java?
For example:
//generate a random integer value
int randomVal = RandomHelper.nextIntFromTo(1, otherVal);
/* then divide the integer value in half... the error I am getting is that its a double, probably
because the number generated isn't an even number, but I NEED it to be an integer. Can I round up?*/
int value = randomVal * 0.5;
You can add 1 then divide by 2 instead of multiplying by 0.5. That way, you avoid a floating point operation followed by a conversion to int.
int value = (randomVal + 1) / 2;
Use Math.ceil():
int value = (int)Math.ceil(randomVal * 0.5);

How can I convert float to integer in Java [duplicate]

This question already has answers here:
How to convert float to int with Java
(9 answers)
Closed 7 years ago.
I need to convert a float number into an integer.
Can Java automatically convert float number into integers? If so, do normal rounding rules apply (e.g. 3.4 gets converted to 3, but 3.6 gets converted to 4)?
You have in Math library function round(float a) it's round the float to the nearest whole number.
int val = Math.round(3.6); \\ val = 4
int val2 = Math.round(3.4); \\ val2 = 3
It is a bit dirty but it works:
double a=3.6;
int b = (int) (a + 0.5);
See the result here
Math.round(3.6) would do that for you.

Round to 2 decimal places [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Round a double to 2 significant figures after decimal point
I have:
mkm=((((amountdrug/fluidvol)*1000)/60)*infrate)/ptwt;
in my Java code. The code works fine but returns to several decimal places. How do I limit it to just 2 or 3?
Don't use doubles. You can lose some precision. Here's a general purpose function.
public static double round(double unrounded, int precision, int roundingMode)
{
BigDecimal bd = new BigDecimal(unrounded);
BigDecimal rounded = bd.setScale(precision, roundingMode);
return rounded.doubleValue();
}
You can call it with
round(yourNumber, 3, BigDecimal.ROUND_HALF_UP);
"precision" being the number of decimal points you desire.
Just use Math.round()
double mkm = ((((amountdrug/fluidvol)*1000f)/60f)*infrate)/ptwt;
mkm= (double)(Math.round(mkm*100))/100;
double formattedNumber = Double.parseDouble(new DecimalFormat("#.##").format(unformattedNumber));
worked for me :)
Multiply by 1000, round, and divide back by 1000.
For basic Java: http://download.oracle.com/javase/tutorial/getStarted/index.html and http://download.oracle.com/javase/tutorial/java/index.html
BigDecimal a = new BigDecimal("12345.0789");
a = a.divide(new BigDecimal("1"), 2, BigDecimal.ROUND_HALF_UP);
//Also check other rounding modes
System.out.println("a >> "+a.toPlainString()); //Returns 12345.08
Try:
float number mkm = (((((amountdrug/fluidvol)*1000f)/60f)*infrate)/ptwt)*1000f;
int newNum = (int) mkm;
mkm = newNum/1000f; // Will return 3 decimal places
Create a class called Round and try using the method round as Round.round(targetValue, roundToDecimalPlaces) in your code
public class Round {
public static float round(float targetValue, int roundToDecimalPlaces ){
int valueInTwoDecimalPlaces = (int) (targetValue * Math.pow(10, roundToDecimalPlaces));
return (float) (valueInTwoDecimalPlaces / Math.pow(10, roundToDecimalPlaces));
}
}

Categories

Resources