-1

Is it possible to find the number at an index of an NSDecimalNumber?

For example is something like this possible?

var a: NSDecimalNumber = 10.00123456789
var indexOfa = a(index: 6)
//indexOfa = 6 (6th position from the left)

I'm basically trying to make a custom rounding function that rounds down on x.xx5 and rounds up on x.xx6.

For example: 67.5558 is rounded to 67.55. 67.5568 is rounded to 67.56.

...Not a duplicate of rounding question. I'm asking specifically here how to find a digit at a specified index of an NSDecimalNumber.

Also the answer you linked doesn't answer my question. I can use the rounding behaviours for NSDecimalNumber but it rounds up on .5 I need it to round down on .5 and up on .6. I can create my own custom rounding function if I could find the index of the 2nd decimal number.

I have a solution that works. But its horrible. There's a few NSDecimalNumber extensions in there but you can probably guess what they are doing. And I'm sure theres a better way...

public func currencyRoundDown() -> NSDecimalNumber {
    let rounded: NSDecimalNumber
    let toThree = self.roundDown(3)
    let lower = self.roundDown(2).adding(0.005)
    if lower.moreThan(toThree) || lower.same(toThree) {
        rounded = toThree.roundDown(2)
    } else {
        rounded = toThree.roundUp(2)
    }
    return rounded
}
LateNate
  • 763
  • 9
  • 22
  • You can research over it, Please look at this question -> https://stackoverflow.com/questions/41744278/count-number-of-decimal-places-in-a-float-or-decimal-in-swift – Sucharu Hasija Dec 02 '17 at 21:03
  • @LateNate do not duplicate your own question. If there's a problem with how you asked the question, _edit_ it. – matt Dec 02 '17 at 21:04
  • I don't agree that it doesn't answer your question. If you think about the answers to https://stackoverflow.com/questions/27338573/rounding-a-double-value-to-x-number-of-decimal-places-in-swift you can see that the answer _is_ there. The point is that your question adds nothing to the mix; you could have figured this out just by searching (and thinking). – matt Dec 02 '17 at 21:07
  • Hey @matt. I checked out that answer before I raised the question. I got that i could use bankers rounding method that would indeed round 1.25 to 1.2. But it wouldn't round 423.66552 to 423.66. It rounds it up to 434.67. I'm trying to round at the 2nd decimal place DOWN on .005 and up on .006. None of the rounding behaviours allow this. I have a solutions that works but its ridiculous. I round 423.66552 down to 2 decimal placed, then compare with original value, if its higher round up and lower round down. there MUST be an easier way of doing it. – LateNate Dec 02 '17 at 22:30
  • I've edited question with my awful solution that's going to give me nightmares. It works. But its horrible. A few NSDecimal extensions but you get the drift. – LateNate Dec 02 '17 at 22:57
  • But _this_ question is specifically "to find the number at an index". – matt Dec 02 '17 at 22:58
  • Thanks @matt. Yeah. It is. Otherwise it was flagged as a duplicate. I've literally been searching for an answer for days and I've looked at every rounding post but I still can't solve my question. So I've solved it in a different way. But in solving it it would be easier if I could just identify the 3rd decimal place. My problem is a rounding issue, The solution I've came up with is an NSDecimalNumber index issue. I'm not a developer. This stuff isn't obvious to me. – LateNate Dec 02 '17 at 23:05

1 Answers1

0

Is it possible to find the number at an index of an NSDecimalNumber?

Let's consider how to do it with a Double. In this rendering, the "index" counts from the decimal point.

Multiply by 10 to the index, to bring the index digit into the 1s place. Now take the remainder modulo 10. That is the digit in question.

    let d = 1234.56789 // the 5 is 1, the 6 is 2, the 7 is 3 ...
    let place = 3 
    let shift = pow(10.0, Double(place)) * d
    let digit = Int(shift) % 10 // 7
matt
  • 515,959
  • 87
  • 875
  • 1,141