How to find a number inside another number and show at which decimal place the number occurs?

Hi everyone!

like I wrote in the title, I would like to know how to find a number inside another number and show at which decimal place the number occurs?

For example:

We have the number PI:

3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148

And then we have another number, for example this one:

14159

Now I want to know on which decimal place exactly this number occurs

The Answer is: 1-5

Because the number we are looking for is showing up on the first decimal place and end on the fifth decimal place. As you can see here:

3.-14159-2653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148


I would like to write a small program with an input field and an output field

Input: Type in any number
Output: Show at what decimal place the number occurs

The funny thing is that I think it should be not that hard to program a query to find the numbers easily. However I dont know where to start…

I guess we need to make a const Pi and then we need to define a random number let OtherNumber. We loop though const PI and check if (if statement) the random number we need to loop though is somewhere available inside of the number PI. Then we need to declare that if (if Statement) this number is showing up then display the decimal place at one which the number occurs.

These are my thoughts right now and I would really aprechiate if someone with more experience can let me know his / her thought process.

Thanks for answers!

Hello!

Convert it to string and search inside it? I would use string.indexOf()

2 Likes

Just for clarification, if the original number would have been:
2363.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148 and the number to find was still 14159, would I be correct that you would still want to return the string value 1-5 and not 4-8? Basically, you only care about where it is found after the decimal place?

2 Likes

@RandellDawson Yes that is correct :slight_smile:

@skaparate That sounds easy lol… and when I have the index I need to add the number of digits from the second number and then I have the answer…

It sounds easy, however I also need an input field to be able to type in the number and then find the index and calculate the digits from the input number…

I will try it tomorrow

1 Like

Hi everyone!

Thanks to @RandellDawson he showed me an easy way to find a number simply with the use of IndexOf. Here is the solution:

It works great, however I need to find the decimal place for 1 000 000 numbers and I dont want to type in all numbers by myself lol :joy:

Is there any way to automate these numbers and save the result inside a CSV file?

Here is how I would do it (theoretically):

We need to generate an array with the numbers 1 - 999 999 and then we need to execute the calculation for each number. Therefore we need to loop though the array we created and then add each single number inside the field “Number to find”, afterwards we want to execute the calculation and save the result inside an online CSV file (for example Google Docs). This should be done for each single number until we reached the end of the array.

Is this the correct way to automate an array of numbers?

Can everything be done with vanilla JS only?

Thanks again for help! :slight_smile: