It depends on what kind of formal writing. If it's that formal, you should have been directed to use a style guide which will answer your question. For general writing, a rule of thumb is to write out ordinary single-digit numbers and use numerals for everything else.
New words, one handy idiom, and a 2-minute quiz — delivered to your inbox to keep your streak alive.