How do I generate fixed width output in this statement ?
Sure it does, it's just that the codes that change the color on your terminal also have a width, and so the string you pass to the first
sprintf call is already more than 8 characters. Try it with
and you'll see an affect.
But the width of terminal codes is esoteric, so you're better off moving the color coding outside of the fixed-width formattng.
printf("[%s]", colored( sprintf("%8s", sprintf("\$%0.2f",$Price) ),'red') )