I would like to round a Double to a certain number of decimals always rounding down.
Example
rounding to .00 => 1.5679999 to 1.56
rounding to .000 => 1.5679999 to 1.567
round(1.5679999 * 100) / 100 //returns 1.57
round(1.5679999 * 1000) / 1000 //returns 1.568
I want a Double not a concatenated String
Just replace round with floor!
As of swift 3.0:
myDouble.rounded(.down)
SWIFT4+
ways to round on different cases
var w = 6.5
w.round(.toNearestOrAwayFromZero)
// w == 7.0
// Equivalent to the C 'trunc' function:
var x = 6.5
x.round(.towardZero)
// x == 6.0
// Equivalent to the C 'ceil' function:
var y = 6.5
y.round(.up)
// y == 7.0
// Equivalent to the C 'floor' function:
var z = 6.5
z.round(.down)
// z == 6.0
Related
I have a function that looks like this
bits = bits * 1.5
So if bits was 1 it would return 1.5 but if the function was run again it would return 2.25 however i would prefer if it were to just round up to 2.5 is that possible?
Maybe try the following function from globalnerdy.com:
In your case you could use bits = roundUp(bits, 0.5) after the multiplication by 1.5.
func roundUp(_ value: Double, toNearest: Double) -> Double {
return ceil(value / toNearest) * toNearest
}
You could do it by doing...
round(x * 3) * 0.5
So multiply by 3. Then round it to the nearest int. Then divide by 2.
So you have still multiplied by 1.5 but it gets rounded to the nearest 0.5
1.5 * 3 = 4.5
Rounded = 5
5 / 2 = 2.5
Which is what you wanted
Edit: if you want to always go up to the nearest 0.5 then use ceiling instead of round.
This question already has answers here:
How to round a Double to the nearest Int in swift?
(9 answers)
Closed 4 years ago.
I try to use swift code to calculate 10 * 75% = 7.5
var b : Int = 10
var a : Int = 75
// result x is 0
var x = b * (a / 100)
However the result is zero. How to get 7.5 result without changing the type and value of a and b?
UPDATE:
I got it right by:
var x: Double = (Double(b) * (Double(a) / 100)) // x is 7.5
Now, how can I round it to 8 as a Int type?
You're using integer (i.e. whole number) arithmetic, when what you want is floating-point arithmetic. Change one of the types to Float, and Swift will figure out that x is also a Float.
var b : Int = 10
var a : Int = 75
var x = Float(b) * (Float(a) / 100.0) // now x is a Float (.75)
For doing arithmetic in Swift, both sides of the equation must be using the same type. Try using this code:
let b = 10.0
let a = 75.0
let x = b * (a / 100.0
print(x)
7.5
To make it round up, use the built in ceil function
print(ceil(x))
8.0
Ok then you just need to do:
var b : Int = 10
let c = (Double(b) * 0.75)
let restult = ceil(c)
I need to create a script that will calculate the overlap orbital of two 1s orbitals. The integral is given by
I tried calculating this using code but my answer is nowhere near the analytic result of S=(1+R+R^2/3)exp(-R). Could someone help me figure where I went wrong?
The code:
import Foundation
var sum: Double = 0.0 //The integral result
var step_size: Double = 0.0000025
var a: Double = 0.0
var R: Double = 5.0
var next_point: Double = 0.0
var midpoint: Double = 0.0
var height: Double = 0.0
var r_val: Double = 0.0
func psi_func(r_val: Double) -> Double {
return exp(-r_val)
}
//Integration
while next_point < R {
next_point = a + step_size
midpoint = a + step_size/2
height = psi_func(r_val: midpoint)
sum += psi_func(r_val: midpoint)*step_size
a = a + step_size
}
print("S = ", 2*3.14159*3.14159*sum) // This is a 3-D orbital, so I multiply by 2*pi*pi
For R = 5.0
My answer: 19.61
Analytic answer: 0.097
Two problems I can see:
I see a single wavefunction and not the product of two in your code
It is incorrect to just do a 1d integral and multiply by 2pi^2 in the end
Try doing a proper 3d integral with the correct integrand.
How correctly extract second valued after coma in Float value.
Example:
var value = 5.435
And I would like to take value second value after coma, that is 3.
How to do this properly?
In case you want to handle both positive and negative values:
(Int)( abs(value) * 100 ) % 10
If you want to keep the sign, just remove abs
Maybe this: (int)( value * 100 ) % 10.
If its always going to be the third decimal, I would do it like this.
var value = 5.435
value *= 100
var digit = value % 10
Mod is an expensive operation, instead do
(Int(value * 100)) - (Int(value * 10) * 10)
In your scenario we get
(Int(5.435 * 100)) - (Int(5.435 * 10) * 10);
(Int(543.5)) - (Int(54.35) * 10);
(543) - (54 * 10);
(543) - (540);
3
I'm developing an iPhone app.
I want to round a Float32 number like this 3.124 to this 3.12. Or a number like 4.2258 to 4.226.
How can I do that?
I want to hold this value on a Float32 variable or on a float variable.
float x = 4.2258;
x = x * 100;
roundf(x);
x = x / 100;
That's one option. You can also use NSNumberFormatter to get what you want.
Which way do you want to round?
x = 0.01 * floorf(0.5 + 100.0 * x);