New math related block

can you add the block

(value of digit () of () :: operators)

It is not in scratch.
If you are wondering how it would work,
(value of digit (3) of (234) :: operators)
would return 200, since the 1st digit (not number) has a value of 200. if this is already possible, please tell me!


You know that block is already supported, right? You don't have to make a custom function for it
value of digit of number script pic

that works fine, for whole numbers, doesn't work for decimals. If you do
(value of digit (1) of (2.5)::operators)
you get 200, it should be 2.0 (or 2).

also, I think he meant the digit from the right, not the left.

bc it relies on letters.

I know. I'm saying what it should do

Were you expecting it to work for decimals? That would be a little tricky because it's not obvious whether to count digits like letters (so the decimal point counts as a "digit" and in 56.78 the 6 is digit 2 and the 7 is digit 4) or whether to count the actual digits consecutively (so the 7 is digit 3).

It's actually tempting to call the ones digit "digit 0" and name digits to the right of the decimal point by negative numbers.

But if you really want a robust treatment of non-integers you have to be prepared to handle things like "45.67e-28" which is too horrible to contemplate. So I'd be happy with just integers, so it's a (very long) one-liner:



Of course I haven't actually tried these, but if I did the math correctly, it's annoying that RTL, which is the "right way" to think about digit numbers (as @ego-lay_atman-bay suggests) has two ugly ±1 blocks, one in each input to ×, whereas LTR doesn't. Of course this reflects the disagreement between how we (correctly) count letter positions in a word and the zero-origin powers of 10.

Edit: I clicked the reply button on @sathvikrias's original post, which is why I said "you" in the first sentence, but of course Discourse thinks it's smarter than I am and didn't put his username up at the top of my reply. :~(

yeah, you're right, never thought of that.

btw, while I was trying to add decimals, I also found out the RTL way


int getDigit(int from, int index)
   return (from / pow(10, index)) % 10;

Why don't you supply the Snap! equivalent?

I wanted to and now i'm publishing it.

You don't have to convert them, Snap! is weakly typed.

That's very useful in a language in which numbers and strings are distinct types, so if you have a number there's no direct way to extract a digit. But in Snap! you don't have to "convert to string"; it's already a string, as far as the user can see.

And if you want to select digits with one-origin indexing, as we do for strings and for lists, then you have to do the same index-1 thing I did.

Your suggestion would be very helpful, though, if the OP wanted digits in a base other than decimal!

Populate it,replace tens with other numbers.

But when doing decimals not wholes thatt doesn't work.

We're going around in circles. What counts as "working" depends on what the OP intends. If thinking of the number as a string of digits-and-maybe-decimal-point, then the digit counting has to count the decimal point. Your solution is good if the OP wants to consider a digit's "position" to be its place value power of 10. But in that case I, at least, wouldn't use the word "position" at all.

Yeah,its ambiguos.
In that SOF topic someone even tryed to get the number of digits!

Stack overflow is great for when some piece of commercial software gives an incomprehensible error message that demands a magic incantation that you don't have to understand. But it's not always as good when a topic requires actual wisdom, imho.

Can we close this topic now?

ask the owner.

I'm asking everyone.

not yet. I just got some ideas of how to do it and would like to share them.