Yes, there is a formula.
And there is also a Position Size Calculator, which does all the work for you — no formula needed.
But, before you go any further, you need to go through the School of Pipsology, which is available[I] for free,[/I] right here on this website. That course will answer about 100 of your most basic, newbie questions — like the ones you asked in your post.
By the way, the answers to your questions are as follows:
[B]If the quote currency in the pair you are trading matches your account currency[/B] (for example, your account currency is USD, and you are trading the EUR/USD, the GBP/USD, etc.), then —
In your first example, the answer is 9 micro-lots (which equals 0.9 mini-lots).
In your second example, the answer is 40 micro-lots (which equals 4 mini-lots).
[B]If the quote currency in the pair you are trading does not match your account currency,[/B] then pip-values would be different from the examples above, and the answers to your two examples would be different.
But, put all this aside for now, until you have gotten a basic forex education, by diligently studying the School of Pipsology.