Search This Blog

Tuesday, October 11, 2016

iOS Swift. Show two decimal places for Double

We can format output for double values with String Format:
let value = 3.1415
let formatValue = String(format:"%.2f", value) // 3.14

Or we can use NumberFormatter Class (setting min and max number of digits after decimal separator):
let formatter = NumberFormatter()
formatter.numberStyle = NumberFormatter.Style.decimal
formatter.roundingMode = NumberFormatter.RoundingMode.halfUp
formatter.maximumFractionDigits = 2
formatter.minimumFractionDigits = 2
        
let roundedValue1 = formatter.string(from: 0.684) // 0.68
let roundedValue2 = formatter.string(from: 0.687) // 0.69
let roundedValue3 = formatter.string(from: 0.600) // 0.60


6 comments:

  1. Really nice article. its really helpful me. Very interesting and good post thanks for sharing such a good blog.
    -Custom Web Design and Development

    ReplyDelete
  2. It was not an easy topic to elaborate in such a short span of time. Custom Website is such a complex topic but you made it easy to understand

    ReplyDelete
  3. This post wowed me with its well-researched content and excellent writing. Because I was so involved in this book, I couldn't put it down. Your work and ability have wowed me. Thank you very much. Those who want to learn more about it may find it useful. Best Custom Websites

    ReplyDelete
  4. Thank you very much for drawing my notice to this. Your blog is jam-packed with useful facts. Until I read that, I would have no idea. I'll come back for more fantastic material.
    Best wishes, and good luck.
    Web Design USA

    ReplyDelete
  5. Good content. I was looking for this type of content. Saved my time for further search.
    You should always provide that type of content. I appreciate your determination to give the students a good title.Custom Designed Websites

    ReplyDelete
  6. Amazing write-up! The blog was very informative. Keep it up!
    Custom Website Development

    ReplyDelete