Swift:打印除​​法的十进制精度 [英] Swift: Print decimal precision of division

查看:40
本文介绍了Swift:打印除​​法的十进制精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试打印除法的结果,例如:

I'm trying print the result of division for example:

let division = (4/6)
print(division) 

在这种情况下,打印输出为 0.

In this case the print out is 0.

如何在不丢失数值的情况下打印除法的数值.我的意思是不将输出转换为字符串.

How can I print the numeric value of the division without losing the numeric value. I mean without casting the output to string.

推荐答案

您正在执行整数除法.您需要执行浮点除法.

You are performing integer division. You need to perform floating point division.

在您的代码中,division 是一个 Int 值.4/6 在整数除法中为零.

In your code, division is an Int value. 4 / 6 is zero in integer division.

您需要:

let division = 4.0/6.0
print(division)

这篇关于Swift:打印除​​法的十进制精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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