This question already has answers here:
Format double value in scientific notation
(4 answers)
Closed 6 years ago.
I want to show factorial of a number in scientific notation.
For example - 100! = "9.332621544e+157"
Is there any other way to do this besides NumberFormat? This is my code so far -
BigDecimal res=new BigDecimal("1");
for(int i=2;i<=x;i++){
res=res.multiply(BigDecimal.valueOf(i));
}
str=String.valueOf(res+"");
display.setText(str);
I'm really sorry if this question is vague to understand. Any help would be appreciated. Thanks in advance.
Not nice but changing
str=String.valueOf(res+"");
to
String str=res.doubleValue()+"";
will do the job
Using NumberFormat is the way to go but if you don't want to use that you could use a PrintStream
See Formatting Numeric Print Output for more info
Related
This question already has answers here:
How do you convert a byte array to a hexadecimal string, and vice versa?
(53 answers)
Closed 4 years ago.
I am looking for a way or multiple ways (Always good to know many ways) how to convert Text (String) to HexaDecimal like this http://codebeautify.org/string-hex-converter
Any help would be appreciated.
I've been looking for hours around different places and I have so far found no code that could potentionally do this for me.
All the ways that this could be done is accepted, I love learning about coding.
String to Hex:
Integer.decode(hexString);
Hex to String:
Integer.toHexString(integer);
This question already has answers here:
How to avoid floating point precision errors with floats or doubles in Java?
(12 answers)
Closed 8 years ago.
In my Java class.
My teacher has asked this question.
Can anyone help me out, I would appreciate it. TY.
why
0.3+0.3+0.3 = 0.899999999999999
in java ?????
Its because of floating point precision errors. The reason is that these data types are built for fast and accurate approximations and not for exact results. For that we use BigDecimal
For more info
Java Types
Java Float Types
This question already has answers here:
Why does Perl's sprintf not round floating point numbers correctly?
(2 answers)
Closed 8 years ago.
So, I made this lines of code to return 10% of the value in the parameter. For example, if the value being given is 7, the code should return 0.7. But it is returning 0.7000000000000001. Any thoughts why? The code is below:
public double calculateDiscount(double price){
double discount;
discount = price*0.10;
return discount;
}
I'm using eclipse IDE, and using the debug, I didn't find anything weird, the multiplication is simply returning that huge number.
PS.: I know that I can return only two decimal cases, but I want to know whats going on.
PPS.: Using /10 instead of *0.10 works just fine. But, as I said, I want to know why.
If you really care about precision, may be you want to use java.math.BigDecimal.
See more in How to Use Java BigDecimal: A Tutorial.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Retain precision with Doubles in java
import static java.lang.System.out;
public class q2{
public static void main(String args[]){
double x=4.02, y=0.05;
out.println(x+y);
}
}
Output:
4.069999999999999
Why is it outputting the that. I thought it would be 4.07. Please explain why this happens in java ?
Sorry for the inaccurate Question title. I can't have a better title than this
That is because some numbers -- such as 0.1 -- cannot be represented exactly in binary floating-point.
Consider reading the following article:
What Every Computer Scientist Should Know About Floating-Point
Arithmetic
You are seeing a rounding error. See How to resolve a Java Rounding Double issue
To resolve it you can change to BigDecimal instead of double as mentioned in the accepted answer to the linked question.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Java: Parse a mathematical expression given as a string and return a number
Hello there,
I would like to ask you, if exist some way how to convert string "1+2+3" to math equation? Exist for this purpose some function or method in Java?
Thanks for hints.
It's not part of the standard API.
But I implemented it in my project, you can have a look here.
https://github.com/MarkyVasconcelos/Towel/wiki/Expression
This would necessarily depend on the implementation of the Equation class you're using. Check its API.