This function already exists as $bytes. To do it manually, you divide by 1024^x, not 1000^x.