Question

Write a stored function that takes a number as its input and returns that number as...

Write a stored function that takes a number as its input and returns that number as currency, i.e. it should have a leading dollar sign, only have two decimal places, and have commas for the thousands. If the number is negative the result should be in parenthesis, accounting format, -123.45 returns ($123.45). If the input is a non-number, e.g. “Hello”, the function should return $0.00

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Since you have not mentioned the language of your preference, I am providing the code in Python.

CODE

def formatCurrency(value):

try:

value = float(value)

if value < 0:

return '(${:,.2f})'.format(value)

else:

return '${:,.2f}'.format(value)

except ValueError:

return "$0.00"

print(formatCurrency(-123.45))

print(formatCurrency(123.45))

print(formatCurrency("Hello"))

Add a comment
Know the answer?
Add Answer to:
Write a stored function that takes a number as its input and returns that number as...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT