This method basically converts decimal to binary but i couldn't trace how this method works.
can anyone explain this to me? especially "math.floor". i really dont understand what it does...
here's the code(method),
public static String convert(int n, int b)
{
int a = 0;
String s = "";
String z = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
while(n != 0)
{
a = n % b;
s = z.charAt(a) + s;
n = (int) Math.floor(n / b);
}
return s;
}
hoping for your replies and help..tnx in advance guys.
pls help me.
badly need help. in Converting decimal to binary number system in java.
Started by flickbyk_cyrus, Jan 08 2010 09:15 AM
3 replies to this topic
#1
Posted 08 January 2010 - 09:15 AM
|
|
|
#2
Posted 08 January 2010 - 09:37 AM
Here is what the declaration on math.floor says
For your first question I am not sure yet.
Quote
* Returns the largest (closest to positive infinity)
* <code>double</code> value that is less than or equal to the
* argument and is equal to a mathematical integer. Special cases:
* <ul><li>If the argument value is already equal to a
* mathematical integer, then the result is the same as the
* argument. <li>If the argument is NaN or an infinity or
* positive zero or negative zero, then the result is the same as
* the argument.</ul>
* <code>double</code> value that is less than or equal to the
* argument and is equal to a mathematical integer. Special cases:
* <ul><li>If the argument value is already equal to a
* mathematical integer, then the result is the same as the
* argument. <li>If the argument is NaN or an infinity or
* positive zero or negative zero, then the result is the same as
* the argument.</ul>
twas brillig
#3
Posted 08 January 2010 - 09:53 AM
this function converts from decimal to any numbering system not just binary,
as for binary if you traced it the line
floor in general approximate the number to the first one below
e.g :2.57---floor--->2
PS: try to trace your codes by placing break points and monitoring memory changes done by the lines of code
as for binary if you traced it the line
a=n%bwith b as base which will be 2 the output will only be 0 or 1 accessing the string with 0 or 1 will return 0 or 1 :) for the case of base 2, so the final string "s" will contain 0 or 1 and the last line
n = (int) Math.floor(n / b);is the normal conversion for decimal to binary where you divide by 2 and take the integer part "thats why used floor".
floor in general approximate the number to the first one below
e.g :2.57---floor--->2
PS: try to trace your codes by placing break points and monitoring memory changes done by the lines of code
#4
Posted 12 January 2010 - 08:20 AM
Are you asking what that code does?
It converts from decimal to any base, The reason it has two loops is so that it can get each number after the other after one (whichever) is input into the program. any can be input because String Z was assigned all possible decimal inputs.
As for your specific question.. math.floor rounds a number DOWN... (hance the .floor, haha get it)
for ex. document.write(Math.floor(0.30)); would be "0"
String s is an empty string, a placeholder, nothing.
The reason a = n%b is because you need to get the remainder of a number after it is divided by 2 so that you can output that remainder. (for ex. if its base2, its going to output the remainders which should all either be a 1 or a 0)
Hope this clears up what the code does for you. And i hope that was your question... hahaha...
-Git
It converts from decimal to any base, The reason it has two loops is so that it can get each number after the other after one (whichever) is input into the program. any can be input because String Z was assigned all possible decimal inputs.
As for your specific question.. math.floor rounds a number DOWN... (hance the .floor, haha get it)
for ex. document.write(Math.floor(0.30)); would be "0"
String s is an empty string, a placeholder, nothing.
The reason a = n%b is because you need to get the remainder of a number after it is divided by 2 so that you can output that remainder. (for ex. if its base2, its going to output the remainders which should all either be a 1 or a 0)
Hope this clears up what the code does for you. And i hope that was your question... hahaha...
-Git


Sign In
Create Account

Back to top









