Beginner 20 min read

Present the Result

By the end of this lesson, you'll be able to:

  • Format a Decimal result into a clean, readable String
  • Combine all four lessons into one finished, working block of code
  • Recognize that presentation is part of a program's job, not an afterthought

Prerequisites: "Handle Invalid Input"

Formatting the output

Decimal result = 17.5;
String formatted = String.valueOf(result.setScale(2));
System.debug(formatted); // "17.50"
  1. Decimal result = 17.5; — a value with only one decimal digit.
  2. String formatted = String.valueOf(result.setScale(2)); — .setScale(2) rounds and pads a Decimal to exactly 2 decimal places, then String.valueOf() converts it to text.
  3. System.debug(formatted); — logs "17.50" — the difference between a calculator that prints 17.5 versus one that consistently prints 17.50, which matters for anything involving money.

Building a full summary message

Decimal a = 12;
Decimal b = 5;
String operatorSymbol = '+';
Decimal result = a + b;

String summary = a + ' ' + operatorSymbol + ' ' + b + ' = ' + result.setScale(2);
System.debug(summary); // "12 + 5 = 17.00"
  1. Decimal a = 12; — first operand.
  2. Decimal b = 5; — second operand.
  3. String operatorSymbol = '+'; — the operation.
  4. Decimal result = a + b; — the actual calculation.
  5. (blank line)
  6. String summary = ... — String concatenation from Module 4, combined with .setScale(2) — turning a raw number into a message a user could actually read at a glance.
  7. System.debug(summary); — logs "12 + 5 = 17.00".

The complete calculator, assembled

Decimal a = 12;
Decimal b = 5;
String operatorSymbol = '+';
String summary;

switch on operatorSymbol {
    when '+' {
        summary = a + ' ' + operatorSymbol + ' ' + b + ' = ' + (a + b).setScale(2);
    }
    when '-' {
        summary = a + ' ' + operatorSymbol + ' ' + b + ' = ' + (a - b).setScale(2);
    }
    when '*' {
        summary = a + ' ' + operatorSymbol + ' ' + b + ' = ' + (a * b).setScale(2);
    }
    when '/' {
        if (b == 0) {
            summary = 'Error: cannot divide by zero.';
        } else {
            summary = a + ' ' + operatorSymbol + ' ' + b + ' = ' + (a / b).setScale(2);
        }
    }
    when else {
        summary = 'Error: unrecognized operator "' + operatorSymbol + '".';
    }
}

System.debug(summary);
  1. Decimal a = 12; — first operand.
  2. Decimal b = 5; — second operand.
  3. String operatorSymbol = '+'; — chosen operator.
  4. String summary; — will hold the final, formatted message.
  5. (blank line)
  6. switch on operatorSymbol { — routes to the matching branch, exactly as in Lesson 2. 7-9. when '+' { ... } — matches here, building and formatting the addition message in one line. 10-12. when '-' { ... } — the subtraction branch, not reached this time. 13-15. when '*' { ... } — the multiplication branch, not reached. 16-22. when '/' { ... } — the division branch, guarded against zero exactly as in Lesson 3. 23-25. when else { ... } — the unrecognized-operator guard from Lesson 3.
  7. } — closes the switch.
  8. (blank line)
  9. System.debug(summary); — logs "12 + 5 = 17.00".

Every piece from this module is here: the planned structure (Lesson 1), the switch-based operations (Lesson 2), the division-by-zero and unrecognized-operator guards (Lesson 3), and clean formatted output (this lesson) — one small, complete, safe program, using nothing but variables, expressions, and a switch statement.

Where this goes next

This block of code works, but notice it only calculates one thing — change a, b, or operatorSymbol and you'd have to run it again. Once Module 9 teaches methods, you'll be able to wrap this exact logic in a method that takes a, b, and operatorSymbol as parameters and returns the summary — callable as many times as you like without retyping anything. And once Module 10 teaches classes, that method will live inside a proper Calculator class. Keep this working version in mind; you'll rebuild it there, and it'll suddenly make a lot more sense why methods and classes exist at all.

Trying a few different inputs

// Change a, b, and operatorSymbol above to any of these, and re-run:

// a = 12, b = 5, operatorSymbol = '+'   -> "12 + 5 = 17.00"
// a = 10, b = 0, operatorSymbol = '/'   -> "Error: cannot divide by zero."
// a = 9,  b = 3, operatorSymbol = '?'   -> "Error: unrecognized operator "?"."
  1. // Change a, b, and operatorSymbol above to any of these, and re-run: — every line here is a comment, not executable code — these are suggested values to try.
  2. (blank line)
  3. // a = 12, b = 5, operatorSymbol = '+' -> "12 + 5 = 17.00" — the happy path.
  4. // a = 10, b = 0, operatorSymbol = '/' -> "Error: cannot divide by zero." — exercises the division-by-zero guard from Lesson 3.
  5. // a = 9, b = 3, operatorSymbol = '?' -> "Error: unrecognized operator "?"." — exercises the unrecognized-operator guard.

Three inputs exercising the happy path, the division-by-zero guard, and the unrecognized-operator guard — all handled cleanly by the same block of code, just with different starting values.

Exercise

Given Decimal price = 49.5, write code that formats it as a String with exactly 2 decimal places using setScale.

Show hint

price.setScale(2)

Salesforce: ○ Not connected Connect
APEX

Execution Results

Run your code to see the result.

Present the Result Quiz

1. What does setScale(2) do to a Decimal?

Log in to submit the quiz and save your score.

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

A correct result that's hard to read is still a UX failure — this final lesson formats the calculator's output cleanly and assembles everything into one finished, working program.