Substring/toString/IndexOf Function not working past a certain value [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 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);

Related

Is there any method of converting math formula 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 2 years ago.
Improve this question
I need to convert that formula in such a way that i can make the calculation by taking the input of radix and number from user
〖log〗_radix (number)= (〖log〗_e (number))/(〖log〗_e (radix))
Well, you could simply read the inputs similar to here. Then, just calculate the result.
Scanner input = new Scanner(System.in);
double number = input.nextDouble();
double radix= input.nextDouble();
double result = Math.log(number) / Math.log(radix);

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();

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);

double , long in Java use [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
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)

Categories

Resources