double , long in Java use [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
double ps1 = (double) (((double)1)/(double)100);
int maz = (double) ((ps1) * Double.parseDouble(500000.102)));
Is this right to use double, or shall i use long?
I am doing large calculations. And need to keep the correctness of the .102.

Use double! Because long has no signs after , (they have no fraction)

Related

How to generate a random mobile number starts with zero and have 10 digits, that starts with zero? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
My Friend ask me this question and i am posting it here, if you have better approach please share
String getRandomMobileNumber() {
double random = Math.random();
Double randomten = random*1000000000.0;
return "0"+Math.round(randomten);
}
You can use this, see javadoc:
String phoneNumber = "0" + ThreadLocalRandom.current().nextInt(10000000, 99999999);

Java: Sum of values inside a Collection of DTO [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Calculate the sum of amountField in MyDTO, Java 8
private Double getSum(List<MyDTO> myDTOList) {
return myDTOList.stream().map(MyDTO::getAmount).reduce(0d, Double::sum);
}
You can use Stream.mapToDouble to convert it into a DoubleStream and then use DoubleStream.sum:
return myDTOList.stream().mapToDouble(MyDTO::getAmount).sum();
To mention, the above shall work for amount being of integer type as well, while the code shared by you and this shall still work fine if the type of amount is already double.
Or as pointed out in comments by you, if your DTO object can be null, you can filter out those values using:
return myDTOList.stream().filter(Objects::nonNull).mapToDouble(MyDTO::getAmount).sum();

Issues with math.ceil [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I need to round up a double to the nearest highest int and I just stumbled across the ceil() method. I'm not quite sure what I'm doing wrong but it is not working as expected. I wrote this code to try to troubleshoot what I'm doing wrong but I can't figure out. I expected this to print '1.0' since .75 rounded up is 1.
int d=3;
int b= 4;
double c=Math.ceil(d/b);
System.out.println(c);
you have to cast it first
double c=Math.ceil((double)d/b);

evaluate expression in java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Could someone please help me with storing -1.0539727e+15 within java. I have never come across having to store such a number so have no idea what the best data type would be
I think you are looking for a double.
Try out this:
double d = 2e-6;
double f = -1.0539727e15;
System.out.println(d);
System.out.println(f);
System.out.printf("d: %f\n", d);
System.out.printf("f: %f\n", f);

Substring/toString/IndexOf Function not working past a certain value [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a function:
ItemValue[i] = substring(toString(obj),0,toString(obj).indexOf(".") + 4);
where obj is a number. The function works for 9,999,999.99999 -> 9999999.999 but values such as 99,999,999.99999 gets converted to 9.999.
Is it a data type issue?
Thanks,
The problem is with the double values you are using. The value 99999999.99999 will be translated to 9.999999999999E7 by toString() so the results you are getting. In order to strip the digits after decimal you can use DecimalFormat class:
DecimalFormat f = new DecimalFormat();
f.setMaximumFractionDigits(3);
f.setMinimumFractionDigits(3);

Categories

Resources