Pages: 1
Topic closed
I want to be able to convert any decimal number into any base (base 10, 16, 32, etc.) specified by a list (if your way uses lists) because I plan on making a converting project.
-------------------------------------------------------------------------------------
I deleted a 5-minute older topic similar to this one, but I decided to make something else instead.
Offline
I'd do it with a formula, rather.
Being ignorant in this subject my way would be count upwards in decimal and add a digit every "base" numbers, but there must be a faster, easier way. Google could help!
Offline
LS97 wrote:
I'd do it with a formula, rather.
Being ignorant in this subject my way would be count upwards in decimal and add a digit every "base" numbers, but there must be a faster, easier way. Google could help!
Yeah, I just had that thought but can't get it working.
Offline
Here's a script:
when gf clicked delete (all v) of [remainders v] ask [Give a decimal number] and wait set [number v] to (answer) ask [Choose a base to convert to] and wait set [base v] to (answer) repeat until <(number) = [0]> set [rounded down v] to [] //mini script from wiki set [count v] to [0] repeat until <<(letter (count) of ((number) / (base))) = [.]> or <(count) = (length of ((number) / (base)))>> set [rounded down v] to (join (rounded down) (letter (count) of ((number) / (base)))) change [count v] by (1) //end mini script end add (item (((number) mod (base)) + (1)) of [in base v]) to [remainders v] set [number v] to (rounded down) end set [count v] to (length of [remainders v]) //mini script to join digits set [new num v] to [] repeat (length of [remainders v]) set [new num v] to (join (new num)(item (count) of [remainders v])) change [count v] by (-1) end say (join (join([Your number in base](base))[ is ](new num))Does that work for you?
Last edited by scimonster (2012-02-02 14:29:01)
Offline
This should work for bases less than 10, for bases greater than ten, you will need to create alphabets to stand in for numbers and modify the script accordingly.
http://i.imgur.com/QUlJy.gif
Offline
I made it work with conversion to binary, but it still isn't working with base 3.
Offline
Sorry everyone, I'm about to explode in confusion.
I'm sure I found a small and simple way, why isn't it working in Scratch?
Offline
demosthenes wrote:
Am I seeing a screenshot there
when green flag clicked set [base v] to (answer) ask [Input a number] and wait set [number v] to (answer) delete (all v) of [value v] repeat (10) add [0] to [value v] end set [difference v] to (number) repeat until <(difference) = [0] set [index v] to [0] set [temp v] to [1] repeat until <(temp) > (difference)> set [temp v] to ((base) * (temp)) change [index v] by (1) end replace item (index) of [value v] with (round (((difference) - ((difference) mod ((temp) / (base)))) / ((temp) / (base)))) set [difference v] to ((difference) - ((item (index) of [value v])*((temp) / (base)))) end
Offline
Did you see my post? It works for anything up to base 36. It would be an even shorter script of Scratch has a (round [up/down v] ()) block.
Last edited by scimonster (2012-02-02 15:01:45)
Offline
scimonster wrote:
Did you see my post? It works for anything up to base 36. It would be an even shorter script of Scratch has a (round [up/down v] ()) block.
Yes, but it's just too long...
Offline
scimonster wrote:
Did you see my post? It works for anything up to base 36. It would be an even shorter script of Scratch has a (round [up/down v] ()) block.
Rounding up or down (called 'floor' or 'ceil' in a lot of programming languages) can already be done in Scratch
The solution is actually fairly simple:
say (round ((value) + (0.5))) //round up say (round ((value) - (0.5))) //round down
Offline
scimonster wrote:
Did you see my post? It works for anything up to base 36. It would be an even shorter script of Scratch has a (round [up/down v] ()) block.
You can just put:
(round ((value) + (0.5)))and
(round ((value) - (0.5)))
Offline
scimonster wrote:
Here's a script:
when gf clicked delete (all v) of [remainders v] ask [Give a decimal number] and wait set [number v] to (answer) ask [Choose a base to convert to] and wait set [base v] to (answer) repeat until <(number) = [0]> set [rounded down v] to [] //mini script from wiki set [count v] to [0] repeat until <<(letter (count) of ((number) / (base))) = [.]> or <(count) = (length of ((number) / (base)))>> set [rounded down v] to (join (rounded down) (letter (count) of ((number) / (base)))) change [count v] by (1) //end mini script end add (item (((number) mod (base)) + (1)) of [in base v]) to [remainders v] set [number v] to (rounded down) end set [count v] to (length of [remainders v]) //mini script to join digits set [new num v] to [] repeat (length of [remainders v]) set [new num v] to (join (new num)(item (count) of [remainders v])) change [count v] by (-1) end say (join (join([Your number in base](base))[ is ](new num))Does that work for you?
You'll need a list called "in bases" that has all the characters in the bases. Item 1 of the list should be 0.
BTW, Google helped. XD
How long did it take you to figure that out?! D:
Offline
JSO wrote:
scimonster wrote:
Did you see my post? It works for anything up to base 36. It would be an even shorter script of Scratch has a (round [up/down v] ()) block.
Rounding up or down (called 'floor' or 'ceil' in a lot of programming languages) can already be done in Scratch
The solution is actually fairly simple:say (round ((value) + (0.5))) //round up say (round ((value) - (0.5))) //round down
I know what it is in other languages. Cool, thanks for the workaround.
RedRocker227 wrote:
scimonster wrote:
Here's a script:
when gf clicked delete (all v) of [remainders v] ask [Give a decimal number] and wait set [number v] to (answer) ask [Choose a base to convert to] and wait set [base v] to (answer) repeat until <(number) = [0]> set [rounded down v] to [] //mini script from wiki set [count v] to [0] repeat until <<(letter (count) of ((number) / (base))) = [.]> or <(count) = (length of ((number) / (base)))>> set [rounded down v] to (join (rounded down) (letter (count) of ((number) / (base)))) change [count v] by (1) //end mini script end add (item (((number) mod (base)) + (1)) of [in base v]) to [remainders v] set [number v] to (rounded down) end set [count v] to (length of [remainders v]) //mini script to join digits set [new num v] to [] repeat (length of [remainders v]) set [new num v] to (join (new num)(item (count) of [remainders v])) change [count v] by (-1) end say (join (join([Your number in base](base))[ is ](new num))Does that work for you?
You'll need a list called "in bases" that has all the characters in the bases. Item 1 of the list should be 0.
BTW, Google helped. XDHow long did it take you to figure that out?! D:
Oh, 5 minutes, once I had the algorithm.
Here's an updated version of the script:
when gf clicked delete (all v) of [remainders v] ask [Give a decimal number] and wait set [number v] to (answer) ask [Choose a base to convert to] and wait set [base v] to (answer) repeat until <(number) = [0]> add (item (((number) mod (base)) + (1)) of [in base v]) to [remainders v] set [number v] to (round ((number) - (0.5))) end set [count v] to (length of [remainders v]) //mini script to join digits set [new num v] to [] repeat (length of [remainders v]) set [new num v] to (join (new num)(item (count) of [remainders v])) change [count v] by (-1) end say (join(join [Your number in base](base))(join [ is ](new num)))
Last edited by scimonster (2012-02-04 12:54:40)
Offline
I'm doing something similar, building a base conversion block in BYOB... it keeps outputting 0.
base (base =13) 2 digit number (orignum =54) to base 10 set [baseout v] to <<letter (1) of (orignum)> + <(base)*<letter (2) of (orignum)>>>report baseout
Last edited by wilsonbiggs (2012-02-04 18:55:10)
Offline
wilsonbiggs wrote:
I'm doing something similar, building a base conversion block in BYOB... it keeps outputting 0.
base (base =13) 2 digit number (orignum =54) to base 10 set [baseout v] to <<letter (1) of (orignum)> + <(base)*<letter (2) of (orignum)>>> report baseout
Try using my script.
Offline
wilsonbiggs wrote:
I'm doing something similar, building a base conversion block in BYOB... it keeps outputting 0.
base (base =13) 2 digit number (orignum =54) to base 10 set [baseout v] to <<letter (1) of (orignum)> + <(base)*<letter (2) of (orignum)>>>report baseout
That's funny... I calculated and the answer should be 56. Use the
report [stuff]in the Control category and place it onto the end of the script.
Offline
Topic closed
Pages: 1