如何获得最多 2 个小数位的 Double 值 [英] How to get a Double value up to 2 Decimal places

查看:55
本文介绍了如何获得最多 2 个小数位的 Double 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我正在处理需要最多两位小数的货币,我使用数字格式化程序来显示最多两位小数,但是当我想要这个双值从 0.7 到 0.70 时,我无法要做到这一点,输入 double 总是给我 0.7,但我想要 0.7 到 0.70

In my app I am dealing with currency which is need to upto two decimal digit , I have used number formatter for this to display up to two decimal place but When I want this double value up like 0.7 to 0.70 , I am unable to to do that , type double is always giving me 0.7 but i want 0.7 to 0.70

推荐答案

你可以简单地这样做:

let pi = 3.14159

let text = String(format: "%.2f", arguments: [pi])

print(text) // output = 3.14

这篇关于如何获得最多 2 个小数位的 Double 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆