如何在小数和小数之间拆分 NSDecimalnumber 整数 & 后退

发布于 2024-07-13 01:13:27 字数 345 浏览 6 评论 0原文

我不知道如何获得积分 & 来自 NSDecimalnumber 的十进制值。

例如,如果我有 10,5,如何得到 10 & 10,5? 5?

如果我有 10 & 5、如何返回10,5?


嗯,好吧,看起来好多了!

但是我担心丢失小数。

这是一个国际化的应用程序。 例如,如果用户设置价格“124,2356”,我想加载& 保存准确的数字。


嗯,好吧,就是这样,但不尊重小数位数。 如果存在一种方法可以从当前本地知道我已设置。

这是因为它代表货币价值......

I can't figure how get the integral & decimal value from a NSDecimalnumber.

For example, if I have 10,5, how get 10 & 5?

And if I have 10 & 5, how return to 10,5?


Mmm ok, that is looking better!

However I have the concer about lossing decimals.

This is for a app that will be international. If for example a user set the price "124,2356" I wanna load & save the exact number.


Mmm ok, that is the way, but not respect the # of decimal places. If exist a way to know that from the current local I'm set.

This is because that represent currency values...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一梦浮鱼 2024-07-20 01:13:27

我使用 2 作为比例,因为您说这是针对货币的,但您可以选择使用其他舍入比例。 我还忽略了舍入可能带来的任何异常,因为这是一个非常简单的示例。

NSDecimalNumberHandler *behavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundDown scale:0 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
NSDecimalNumber *price = /* eg. 10.5 */;
NSDecimalNumber *dollars = [price decimalNumberByRoundingAccordingToBehavior: behavior];
NSDecimalNumber *cents = [price decimalNumberBySubtracting: dollars];

这将分别为 dollarscents 变量提供 10 和 0.5。 如果您想要整数,您可以使用此方法将您的乘以 10 的幂。

cents = [cents decimalNumberByMultiplyingByPowerOf10: 2];

这反过来会将您的乘以 100,得到 10 和 5 美元美分。 您还应该知道,这里可以使用 10 的负幂来除法。

因此,

cents = [cents decimalNumberByMultiplyingByPowerOf10: -2];

将撤消最后一个方法。

I'm using 2 for the scale since you said this was for currency but you may choose to use another rounding scale. I'm also ignoring any exceptions rounding may bring since this is a pretty simple example.

NSDecimalNumberHandler *behavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundDown scale:0 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
NSDecimalNumber *price = /* eg. 10.5 */;
NSDecimalNumber *dollars = [price decimalNumberByRoundingAccordingToBehavior: behavior];
NSDecimalNumber *cents = [price decimalNumberBySubtracting: dollars];

That will give you 10 and 0.5 in the dollars and cents variables respectively. If you want whole numbers you can use this method to multiply your cents by a power of 10.

cents = [cents decimalNumberByMultiplyingByPowerOf10: 2];

Which will in turn multiply you cents by 100, giving you 10 and 5 in dollars and cents. You should also know that you can use negative powers of 10 here to divide.

So,

cents = [cents decimalNumberByMultiplyingByPowerOf10: -2];

would undo that last method.

陌伤ぢ 2024-07-20 01:13:27

不确定 Objective c 有什么数学函数,但有

Math.floor(10.5) = 10
(10.5 - 10) * 10 = 5

一个更清晰的例子(10 的余弦)

Math.floor(20.4) = 20
(20.4 - 20) * 10 = 4;

另一个问题

(5 / 10) = .5
10 + .5 = 10.5

小数点上使用的 10 取决于小数位数......例如

Math.floor(20.44) = 20
(20.44 - 20) * 100 = 44;

要求您乘以 100

Not sure what Math functions objective c has but something like

Math.floor(10.5) = 10
(10.5 - 10) * 10 = 5

bit of a clearer example (cos of the 10's)

Math.floor(20.4) = 20
(20.4 - 20) * 10 = 4;

The other problem

(5 / 10) = .5
10 + .5 = 10.5

The 10 used on the decimal points depends on the amount of decimal places... for instance

Math.floor(20.44) = 20
(20.44 - 20) * 100 = 44;

requires you to times by 100

你是我的挚爱i 2024-07-20 01:13:27

如果您无权访问数学 fns,这可能会起作用:

double val = [dn doubleValue]; // dn is a NSDecimalNumber*
int intPart = (int) val;
double doublePart = (val - intPart); 

这可能会给您带来一些麻烦,循环可能永远不会终止,在这种情况下您必须在其中放置一个计数器:

while (doublePart != (int)doublePart) doublePart*=10;

If you don't have access to the math fns, this may work:

double val = [dn doubleValue]; // dn is a NSDecimalNumber*
int intPart = (int) val;
double doublePart = (val - intPart); 

This may give you some trouble, the loop may never terminate, in that case you will have to put a counter in there:

while (doublePart != (int)doublePart) doublePart*=10;
谁人与我共长歌 2024-07-20 01:13:27

double Floor (double x); 是一个 POSIX 函数。 该函数可用于查找不大于x的最大整数值。

double x = 10.5;

double y = floor (x);
// y = 10.0

double z = x - y;
// z = 0.5

double floor (double x); is a POSIX function. This function can be used to find the highest integer value not greater than x.

double x = 10.5;

double y = floor (x);
// y = 10.0

double z = x - y;
// z = 0.5
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文