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"
Decimal result = 17.5;— a value with only one decimal digit.String formatted = String.valueOf(result.setScale(2));—.setScale(2)rounds and pads aDecimalto exactly 2 decimal places, thenString.valueOf()converts it to text.System.debug(formatted);— logs"17.50"— the difference between a calculator that prints17.5versus one that consistently prints17.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"
Decimal a = 12;— first operand.Decimal b = 5;— second operand.String operatorSymbol = '+';— the operation.Decimal result = a + b;— the actual calculation.- (blank line)
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.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);
Decimal a = 12;— first operand.Decimal b = 5;— second operand.String operatorSymbol = '+';— chosen operator.String summary;— will hold the final, formatted message.- (blank line)
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.}— closes theswitch.- (blank line)
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 "?"."
// 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.- (blank line)
// a = 12, b = 5, operatorSymbol = '+' -> "12 + 5 = 17.00"— the happy path.// a = 10, b = 0, operatorSymbol = '/' -> "Error: cannot divide by zero."— exercises the division-by-zero guard from Lesson 3.// 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)
Present the Result Quiz
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.