How to update % printed to Console from MongoDB?

To update and print percentage values to console from MongoDB script, create a variable with your numeric value and use the print() method with toFixed() to format decimal places.

Syntax

var variableName = numericValue;
print(variableName.toFixed(decimalPlaces) + " %");

Example

Create a variable with a percentage value and format it to 2 decimal places ?

var amount = 10.58945;
print(amount.toFixed(2) + " %");

The output of the above code is ?

10.59 %

Key Points

  • Use toFixed() to control the number of decimal places displayed.
  • The print() method outputs directly to the MongoDB shell console.
  • Concatenate the "%" symbol using the + operator.

Conclusion

Use print() with toFixed() to display formatted percentage values in MongoDB shell. This method provides clean console output with controlled decimal precision.

Updated on: 2026-03-15T02:08:34+05:30

171 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements