Removing numbers using NSnumberformatter from a calculated number - ios

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.

Related

How can I change object's size according to the distance/depth in ARView/ARKit

I am working in ARKit/RealityKit/Swift, If I keep one box near to the camera then it has a perfect size. But if I move it far from the camera e.g 1 or 2 meters far then it looks smaller. I want to keep its size same in the user's eyes/perception.
I have done some basic math on the Z coordinate of the object. e.g. if z = -0.9 and size = 0.5 meter then what will be new size if z = -2 or far. But it does not work.
//Sample code to get an idea
let original = Position()
let origianlSize: Float = 0.5
let newPosition = Position() // Assume it is far from original position
let newSize = origianlSize * newPosition.z / original.z // New size is not proper

How do I create a buffered LineString with a width defined in meters when using GEOSwift?

I'm hoping to find a way using GEOSwift to take a series of user's LatLngs, construct a linestring and buffer it to always be 30 meters wide regardless of the user's location. I feel like there must be an easier way than the path I'm going down and any help would be appreciated.
Background:
From what I can tell the buffer functions width parameter is currently defined in decimal degrees as my coordinate system is EPSG 4326, which makes calculating the width in meters difficult. I can get a rough estimation of meters per decimal degree for both longitude or latitude with the Haversine formula.
The problem I have is the series of points can move both latitudinally and longitudinally. So the buffer width I need in these cases lies somewhere between ThirtyMetersInLatDegrees and ThirtyMetersInLngDegrees. And in this case the width to supply to the buffer function becomes a weird approximation/ average of the user's overall longitudinal and latitudinal movement throughout the linestring related to ThirtyMetersInLngDegrees and ThirtyMetersInLatDegrees.
i.e. assuming ThirtyMetersInLngDegrees is the max:
ThirtyMetersInLatDegrees <= bufferWidth <= ThirtyMetersInLngDegrees
How can I better accomplish this?
Here's how I'm calculating meters per decimal degree:
//Earth’s radius
let R=6378137.0
let deviceLatitude = 37.535997
let OneMeterInLatDegrees = 1/R * (180/Double.pi)
let OneMeterInLngDegrees = 1/(R*cos(Double.pi*deviceLatitude/180)) * (180/Double.pi)
let ThirtyMetersInLatDegrees = 30 * latDegreesPerMeter
let ThirtyMetersInLngDegrees = 30 * lngDegreesPerMeter

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.

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

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

Swift - Calculating Percentage of Circle Filled

I am using a framework to display a progress circle with animations. However, I am having some trouble displaying a percentage label in the center of it with the correct percentage. No matter what I try, it says 0%.
Here is the code that calculates the percentage:
var circleAngle = progress.angle
var decimal = circleAngle/360
var percent = decimal*100
percentageLabel.text = String(percent) + "%"
progress is the variable referring to the circle view itself, and it has an angle property that returns the circle's angle. To debug, I ran some print statements, and I found that when I printed decimal, the value was 0. Any ideas on a fix?

Resources