How to round a double to the nearest hundredth in Swift? - ios

I am making an app, which takes one number usually something like 10.00 or 100.00 and multiplies it by 0.15 or 0.20 and outputs it to a Label.
What ends up happening in some situations is the answer comes out to be 2.3234342, notice all the numbers after the decimal, which I don't want.
I want Swift automatically round up to the nearest hundredth or keep hundredth the same and then delete everything after the hundredth.
I want the code to automatically determine whether it will round up or keep the number the same.

You can use round() with a "scale factor" of 1000:
let x = 14.14910001
let y = round(1000.0 * x) / 1000.0
println(y)
May be it will help you.

someFloat+=0.005
label.Text = String(format: "a float number: %.02f ", someFloat))

Related

Divide two numbers and return fraction in swift

I know this is a simple question, but i would like to get a result like this.
3/6 = 0.500000
Divide two numbers and return quotient and reminder in a single variable, how can i achieve above in swift ?
To get the quotient and remainder of a division, you can use the quotientAndRemainder(dividingBy:) function.
3.quotientAndRemainder(dividingBy: 6) // (quotient 0, remainder 3)
If you want to get the floating point result of a division, use the / operator on two floating point numbers.
Either do
let result = 3.0 / 6.0 // 0.5
or if your integers are coming from variables, do
let result = Double(3.0) / Double(6.0) // 0.5

iOS -Chart: Y- Axis interval difference between value

I want to make Y-axis interval with a certain difference. Let's say the multiple of 100 or any fix value, currently it draws with their own interval.
I am sharing my formatting code here.
let leftAxis = chartView.leftAxis
leftAxis.valueFormatter = IntAxisValueFormatter()
leftAxis.granularityEnabled = true
leftAxis.granularity = 1
I am able to solve my challenge, I am adding my fix here. First I calculated the total range then divided them by interval diff and set the LabelCount.
let totalRange = maxY - minY
let interval = 100 //Let's say 100 in my case
leftAxis.setLabelCount(Int(totalRange/interval) + 1, force: true)
One way is you calculate scale value according to your data. And you can use following function to scale as you need. But exact with value difference I don't think it is possible.
self.lineChart.setScaleMinima(1.0, scaleY: 2.0)
In above value first parameter will zoom in horizontally while second will zoom vertically.

Find digit at index of NSDecimalNumber

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
}
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

Removing numbers using NSnumberformatter from a calculated number

I have a calculation that calculate square meters from millimeters and for this to show corect I would like to remove all numbers after the 2 first digits.
This is what I have tried so far.
let spha = sizeh.wshight()
let spwa = sizew.wswidth()
let areas1 = areaModel(pwa:spwa, pha:spha)
let formatter5 = NSNumberFormatter ()
formatter5.maximum = 2
let scarea = formatter5.stringFromNumber(areas1.sarea())!
screenAreaLabel?.text = "\(scarea)sqm"
I am a little lost at this point, I am using Float in calculation of this area.
Hope someone could help me in the right direction.
More detail info added.
I would like the Screen Area to only show 24sqm not 24772610sqm
You are multiplicating a value in millimeter by another value in millimeters, therefore the result is in square millimeters.
To convert square millimeters to square meters, just divide the result by 1_000_000.
You could also set formatter.multiplier to 1.0 / 1_000_000.

Increase Character speed from a Joystick Input?

Now i have a working Joystick, but I want to increase the characters speed.
I´m using this one at the moment: http://roadtonerdvana.wordpress.com/2013/09/20/jcinput-a-simple-joystick-for-sprite-kit/
I attached an Imagefile as the moving object, but it´s way too slow for my purpose.
Where and how can I change the speed?
If you read the post you'll know that the joystick returns a x and y value, each ranging from -1 to 1.
Every time you move the joystick, the properties x and y of it change accordingly, the maximum value is 1 and the minimum is -1.
In the update method, you could do something like this to adjust the speed :
-(void)update:(CFTimeInterval)currentTime {
// I'm using the magic number of 5 as an example of how to magnify the speed x5
float speedX = 5 * self.joystick.x;
float speedY = 5 * self.joystick.y;
[self.myLabel setPosition:CGPointMake(self.myLabel.position.x+speedX, self.myLabel.position.y+speedY)];
}

Resources