问题标签 [nsdecimalnumber]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
4 回答
1616 浏览

objective-c - Prevent scientific notation with NSDecimalNumber output as NSString

I have a problem similar to the one in this question: How to obtain an unformatted string representation of an NSDecimal or NSDecimalNumber? I need to have a number in string format accurately represented as an NSNumber, but this number is converted back to a string in other places, and I can't avoid that. The problem I'm having is that when the number is converted back to a string, the string is in scientific notation in some cases.

How can I prevent this number from being displayed in scientific notation?

I am working in a circa 2005 version of GNUstep (possibly v1.11.1 for GNUstep Base), rather than on mac or iPhone, which may account for some of the differences in behavior vs the other question I referenced. I have looked over the base library API and the documentation in my own GNUstep install, but I can't seem to find anything to help.

EDIT 2/7/12:

The question has changed slightly, but the goal is still the same. Originally, I didn't think I was able to control the string output piece, but I can pass the value back as a string. At this point I am attempting to use a formatting string, but I still want to prevent the scientific notation from appearing.

I've chosen to use %g because we would like a specific number of significant digits for the value. If I use %f, I can trim the extra zeros, but the number does not always come out cleanly. 800000000.79 appears as 800000000.7899999600, for example.

Is there a way to get a cleanly formatted number with up to a certain number of significant digits (or decimal places) without displaying scientific notation before that number of digits?

I'm willing to accept C advice as well.

0 投票
4 回答
17610 浏览

objective-c - 比较 NSDecimalNumber 的不同方法

例如,对于原始的,我会这样做

使用 NSDecimalNumber,这就是我所拥有的

这种比较还有其他方法(更简单,更优雅)吗?如果我将值转换为原始值,我使用什么原始值?我不想使用 CGFloat、float 或 double,因为我在这里处理货币。或者,如果我确实将它们转换为上述那些,有人可以验证/解释精度吗?

0 投票
1 回答
1366 浏览

iphone - 在 UITableView 中创建运行总和

我有一个 UITableView 填充(以前通过导航栏中的按钮保存),带有事务。每行有五个 UILabel:日期、描述、人员、价值(存款和取款)余额。该表按日期排序。如何获取每日余额?余额是从第一个输入到当前行的总和(+ 和 -)。

我有以下代码(JournalDetail.m),但我只得到与价值相同的金额,而不是累积金额(余额)

你能帮我找出实现平衡值的正确方法吗?提前致谢。非常感谢您的帮助。

这是 self.fetchedResultsController 的代码

0 投票
2 回答
4493 浏览

ios - iOS中的NSDecimalNumber错误的小数分隔符

我尝试通过以下方式输出具有正确小数分隔符的十进制数的描述:

输出是:

小数点分隔符应该是 ',' 而不是 '.' 对于这个语言环境。错误在哪里?如何根据本地输出正确的小数分隔符?

0 投票
2 回答
210 浏览

iphone - 非常奇怪的iPhone货币加减法需要帮助

由于我的应用程序今天刚刚上线,这有点“紧急”。

我的应用程序在模拟器中运行良好,当我将其复制到手机、iPad 和其他 iPhone 时运行良好。我们对应用程序进行了测试。我将它提交给了应用商店,现在它已获得批准并生效,但只有下载的应用商店应用才会出现错误。

基本上我正在使用 NSDecimal 和数字格式化程序重新平衡账单。当我单步执行代码时,在模拟器和手机中一切正常。但应用商店捆绑假发。似乎我正在添加和减去的数据没有被初始化。

有没有人见过这样的事情?

我知道这篇文章完全含糊不清,但如果模拟器中的代码工作正常,我想弄清楚从哪里开始调试。

--UPDATE--添加代码

}

0 投票
3 回答
1315 浏览

iphone - 同时乘以 3 个 NSDecimalNumber(s)

首先,我使用 NSDecimalNumbers 因为我正在处理货币。我对十进制数本身没有任何问题,我想知道是否可以同时乘以 3 个 NSDecimalNumber(s)。

换句话说,如果我将 3 个整数相乘,我会:

但是,为了将 NSDecimalNumbers 相乘,您必须使用该decimalNumberByMultiplyingBy 方法

使用这种方法,我必须进行 2 次计算才能将我正在处理的 3 个 NSDecimalNumbers 相乘,如下所示:

有没有办法同时乘以所有 3 个 NSDecimalNumbers 而不是这种笨拙的方法?

0 投票
2 回答
2802 浏览

iphone - 如何计算还款额

嘿,我正在尝试根据贷款金额、利率和年数计算每月还款额。

我想出了以下内容,但是我的计算与互联网上的其他贷款计算器之间似乎存在差异。我的测试数据是 1000 美元的贷款金额,5% 的利息,为期 1 年。这给了我 85.61 的每月付款和 1027.29 的总付款

这是计算这个的代码。

我使用的公式是正确的还是有错误的?

任何帮助,将不胜感激。

非常感谢你

0 投票
1 回答
998 浏览

iphone - 如何将巨大的 NSDecimal 格式化为字符串?

得到了一个NSDecimal高精度的大。像这样:

我可以很容易地得到我的字符串表示NSDecimal

或者

但它从未正确格式化以在屏幕上输出:

我希望它具有像 999,999,999,999.999999999999 这样的组分离

所以我尝试了一个NSNumberFormatter

结果是这样的:

有没有办法NSDecimal在不丢失精度的情况下根据用户区域设置正确格式化高精度?

0 投票
0 回答
217 浏览

core-data - 如何在核心数据中设置十进制类型的精度和小数位数?

您知道如何正确设置小数类型的精度和比例吗?

我可以为属性选择十进制类型,但我看不到在哪里设置精度和比例。

是否可以在迁移期间添加一些纯 sql 查询来更改表(设置 DECIMAL(规模,精度))?或者我应该根据想要的比例和精度创建带有尾数和指数的 NSDecimalNumber(这意味着我必须自己计算所有内容,我不确定数据库站点上发生了什么)

在此先感谢, 安妮娅

0 投票
1 回答
1672 浏览

objective-c - Objective - C,使用 NSDecimalNumberHandler 和 NSDecimalNumberBehaviors 协议

对于那些学习 Objective-C 的人来说,计算器课似乎是一个流行的起点。所以在我的“计算器”类中,我定义了我的加法函数以使用“decimalNumberByAdding:withBehavior:”方法。'_behavior' 在 init 方法中定义。描述的代码如下所示:

要正确使用 NSDecimalNumberBehaviors 协议,需要实现 3 个方法:'-roundingMode'、'-scale' 和 '-exceptionDuringOperation:error:leftOperand:rightOperand:'(但是,上面的 NSDecimalNumberHandler 中没有指定 roundingMode 和 scale,称为'_行为'?)。

使用此协议的动机是如果出现“_behavior”中定义的错误之一(例如溢出或精度丢失错误),则引发并处理异常。

在搜索网络和 Apple 的文档后,尚不清楚如何使用 NSDecimalNumberBehaviors 协议,以及如何正确引发和处理上面“_behavior”中描述的错误(与“_behavior”相比,协议实现似乎包括冗余' 宣言)。

使用 NSDecimalNumberBehaviors 协议并引发和处理“_behavior”中定义的错误的正确方法是什么?也许一个例子最有帮助。谢谢 : )

- 更新:

再次查看文档后,我现在看到 NSDecimalNumberHandler 采用了 NSDecimalNumberBehaviors 协议。所以这意味着我只需要正确初始化和使用 NSDecimalNumberHandler 。

添加除法方法后(为了测试除以 0 异常),我看到代码看起来更像这样:

实际上,除以 0 会引发未捕获的异常并终止程序。好的,这样更好。我如何捕获抛出的异常,以及在语法上,在哪里?