如何快速舍入NSDecimalNumber? [英] How to round an NSDecimalNumber in swift?

查看:88
本文介绍了如何快速舍入NSDecimalNumber?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在此上找不到任何资源,我一直在尝试各种方法,但是没有任何效果.

I can't find any resources on this, and I've been trying all sorts of stuff, but nothing works.

根据Apple的文档,您将NSDecimalNumber如下舍入:

According to Apple's documentation, you round an NSDecimalNumber like this:

NSDecimalNumber.decimalNumberByRoundingAccordingToBehavior(<#behavior: NSDecimalNumberBehaviors?#>)

它包含一个NSDecimalNumberBehavior,我不确定该如何操作,因为(1)无法将其初始化为变量并且更改了属性,并且(2)根据文档所述的roundingMode()方法没有接受任何参数,但Xcode会为"self"填充参数空间.

It takes in an NSDecimalNumberBehavior, which I'm unsure how to manipulate since it (1) cannot be initiated into a variable and have it's properties changed, and (2) the roundingMode() method according to the documentation doesn't take any parameters, but Xcode fills in a parameter space for "self".

我对此完全迷失了.回到基本问题;如何快速对NSDecimalNumber进行四舍五入?

I'm totally lost on this. Back to the basic question; How can I round an NSDecimalNumber in swift?

预先感谢

推荐答案

您可以这样做

let x = 5
let y = 2
let total = x.decimalNumberByDividingBy(y).decimalNumberByRoundingAccordingToBehavior( NSDecimalNumberHandler(roundingMode: NSRoundingMode.RoundUp, scale: 0, raiseOnExactness: false, raiseOnOverflow: false, raiseOnUnderflow: false, raiseOnDivideByZero: false))   

这篇关于如何快速舍入NSDecimalNumber?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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