Format double to 2 decimals

Given a double value val, the twoDecimals method should return a string version of val rounded to exactly two decimal places. There should always be exactly two decimals, even when one or both are zero.

Examples:

twoDecimals(1.23456) => "1.23"
twoDecimals(1.0) => "1.00"
twoDecimals(1234.7777) => "1234.78"

Hints: You're probably going to want to use String.format() for this. It's very similar to printf except that it returns the string instead of outputting it.

Login / Register

To try your hand at solving this problem you'll need an account on CompSci.Rocks. Please either login or register to continue.

Once you've logged in you'll be able to enter code directly into this page to solve the problem.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *