http://www.lintcode.com/en/problem/binary-representation/
Given a (decimal - e.g. 3.72) number that is passed in as a string, return the binary representation that is passed in as a string. If the fractional part of the number can not be represented accurately in binary with at most 32 characters, returnERROR.
Example
For n ="3.72", return"ERROR".
For n ="3.5", return"11.1".
Solution
1 | public class Solution { |
ATTENTION
floatis not available here since Java has taken the first 6 numbers after the point and then rounded up the rest.floatis less precise than adouble.int integer = Integer.parseInt(n.substring(0,n.indexOf('.')));double fraction = Double.parseDouble(n.substring(n.indexOf('.')));