Build a Currency Converter in Ruby

1
2326

In this coding exercise you’ll learn how to build a currency converter method in Ruby, including how to utilize advanced string interpolation.

Summary

Build a currency converter method that can format the currency for the: US, Japan, and UK based currencies.

Exercise File

Code File

Example Input

5000, 'US'

Write a program that formats the data into a location based currency.

Example Output

"$5000.00"

Real World Usage

This is a common coding interview question since it forces you to work with multiple scenarios and perform string manipulation. This is also important when it comes to understanding how to utilize varying string interpolation options related to financial data.

Solution

Can be found on the solutions branch on github.

1 COMMENT

  1. I found by linting that this would be preferred.

    when ‘US’ then format(‘$%.2f’, amount)
    when ‘Japan’ then format(‘¥%.0f’, amount)
    when ‘UK’ then format(‘£%.2f’, amount).tr(‘.’, ‘,’)

LEAVE A REPLY

Please enter your comment!
Please enter your name here