1

How do I ensure that there are only 2 decimal places for this equation (which is converted into a string) in swift.

let avocadoE = ((Double(proteinIntake)!/4.73) * (1/Double(list.count)))

let avocadoP = String(avocadoE)

The values proteinIntake and lis.count are already defined in my code. I can't seem to find any solutions online.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Acer
  • 73
  • 1
  • 3
  • You are better off using a `NumberFormatter` to get the proper, localized result. None of the answers in the duplicate do that. – rmaddy May 25 '17 at 17:17

1 Answers1

0

Try this let avocadoP = String(format: "%.02f", avocadoE)

Bilal
  • 18,478
  • 8
  • 57
  • 72