Unit Price Calculator - Problem

You are shopping at a grocery store and need to calculate the unit price of an item to compare value between different products.

Given the total price of an item and its quantity, calculate the unit price (price per single unit) rounded to 2 decimal places.

Formula: unit_price = total_price / quantity

Input: Two numbers - total price and quantity

Output: Unit price rounded to 2 decimal places

Input & Output

Example 1 — Basic Calculation
$ Input: totalPrice = 12.50, quantity = 5
Output: 2.50
💡 Note: Unit price = 12.50 ÷ 5 = 2.50 per item
Example 2 — Rounding Required
$ Input: totalPrice = 10.00, quantity = 3
Output: 3.33
💡 Note: Unit price = 10.00 ÷ 3 = 3.333... rounds to 3.33
Example 3 — Exact Division
$ Input: totalPrice = 8.00, quantity = 4
Output: 2.00
💡 Note: Unit price = 8.00 ÷ 4 = 2.00 exactly

Constraints

  • 0.01 ≤ totalPrice ≤ 1000.00
  • 1 ≤ quantity ≤ 1000
  • Result rounded to exactly 2 decimal places

Visualization

Tap to expand
INPUT DATATotal Price$12.50Quantity5 itemsALGORITHM STEPS1Read total price and quantity2Divide: 12.50 ÷ 5 = 2.503Apply precision rounding4Format to 2 decimal placesFINAL RESULTUnit Price$2.50per itemEach item costs $2.50Key Insight:Unit price calculation is simple division with proper rounding - divide total by quantity and format to 2 decimal places for currency.TutorialsPoint - Unit Price Calculator | Direct Division Approach
Asked in
Amazon 15 Walmart 12
12.0K Views
Medium Frequency
~5 min Avg. Time
450 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen