Swift returning 0 on division - swift

I'm trying to do basic division and it always returns 0 as an answer.
let mathStuff = Double((stepCount / Level.expRequired())) * 100
print ("\(totalSteps) / \(Level.expRequired()) * 100 = \(mathStuff)")
My print returns
2117 / 2500 * 100 = 0.0
I've tried using NSDecimal instead of a Double and have also tried not using Double or NSDecimal and having it just do the math, which comes back as 0 instead of 0.0.
I'm really confused on what I'm doing wrong here, this seems like basic math and I'm not sure why I'm always given 0 as an answer.

Your problem probably lies here: 2117 /2500, both 2500 and 2117 are Ints.
If they were Double, then it would work: 2117.0 /2500.0 ==> produces non-zero division
Try casting those variables to double first, and you don't need to cast the result itself:
Double(stepCount) /Double(Level.expRequired()))*100
In fact, I believe only one needs to be cast:
Double(stepCount)/Level.expRequired())*100

Related

How I can get the other part after a division with a modulo operator

When I divide 13 with 3 and use integer numbers the result will be 4.
With mod(13,3) I receive the remainder 1. But how can I get the 4 in Matlab? I think it is not possible to switch to integer numbers for this calculation, isn't it?
You can use the floor function:
result = floor(13/3)
This function always rounds down to the lower integer
You can explicitly use integers:
result = uint32(13)/unit32(3);
You can also use hex numbers:
result = 0xDu32 / 0x3u32;
Note that result will be of type uint32.
Use idivide:
result = idivide(13, 3);
You can specify the rounding method with a third argument, with the default being 'fix', or rounding towards zero. For example, this would round towards negative infinity:
result = idivide(13, 3, 'floor');

Dividing UInt64 always returns 0 [duplicate]

I'm trying print the result of division for example:
let division = (4/6)
print(division)
In this case the print out is 0.
How can I print the numeric value of the division without losing the numeric value. I mean without casting the output to string.
You are performing integer division. You need to perform floating point division.
In your code, division is an Int value. 4 / 6 is zero in integer division.
You need:
let division = 4.0/6.0
print(division)
ans = Double(no1)/Double(no2)
return ans
If you want the value should be correct, then try as
let division = Float(v1) / Float(v2)
print(division)

Rounding to two decimal places is not working

I am trying to reduce the decimal places of my number to two. Unfortunately is not possible. For this reason I added some of my code, maybe you will see the mistake...
Update [dbo].[company$Line] SET
Amount = ROUND((SELECT RAND(1) * Amount),2),
...
SELECT * FROM [dbo].[company$Line]
Amount in db which I want to change:
0.00000000000000000000
1914.65000000000010000000
376.81999999999999000000
289.23000000000002000000
Result I get after executing the code:
0.00000000000000000000
1366.28000000000000000000
268.89999999999998000000
206.38999999999999000000
Result I want to get (or something like this):
0.00000000000000000000 or 0.00
1366.30000000000000000000 or 1366.30
268.99000000000000000000 or 268.99
206.49000000000000000000 or 206.49
RAND() returns float.
According to data type precedence the result of multiplying decimal and float is float, try:
ROUND(CAST(RAND(1) as decimal(28,12)) * Amount, 2)
this should do the trick.

Select only two decimal places without rounding up

I want to select only two decimal places without rounding up.
$d = 123000.1264
'{0:f2}' -f $d
Result: 123000,13, but I need the result 123000,12
Any ideas to solve this problem?
Thank you in advance!
[Math]::Truncate(123000.1264 * 100) / 100
does it.
123000.1264 * 100 = 12300012.64
[Math]::Truncate(12300012.64) = 12300012
12300012 / 100 = 123000.12
You should use the [decimal] type for numbers when you need to preserve the accuracy of the fractional part, e.g.
$d = [decimal]123000.1264
and then [Math]::Truncate will use its decimal overload to give a decimal, and a decimal divided by an integer (or a double) will give a decimal result.
Of course, there is more than one way to interpret "up": it could mean increase in value (3 > -5) or increase in magnitude (|-5| > |3|). If you need the former, then use [Math]::Floor (which converts -1.1 -> -2.0) instead of [Math]::Truncate (which converts -1.1 -> 1.0).

Performing operations on a double returns 0

I have a method that receives a number in a NSString format.
I wish to convert this string to a double which I can use to calculate a temperature.
Here's my method.
NSString *stringTemp = text; // text is a NSString
NSLog(#"%#",stringTemp); // used for debugging
double tempDouble = [stringTemp doubleValue];
NSLog(#"%f",tempDouble); // used for debugging
Please note I put the NSLog commands here just to see if the number was correct. The latter NSLog returns a value of 82.000000 etc. (constantly changes as it's a temperature).
Next I wanted to use this double and convert it to a Celsius value. To do so, I did this:
double celsiusTemp = (5 / 9) * (tempDouble - 32);
Doing this: NSLog(#"%d", celsiusTemp); , or this: NSLog(#"%f", celsiusTemp); both give me a value of 0 in the console. Is there any reason why this would be happening? Have I made a stupid mistake somewhere?
Thank you for your help!
Try doing (5.0 / 9.0). If you only use an int to do math where you are expecting a double to be returned (like 0.55) everything after the decimal place will be lost because the cpu expects an int to be returned.
5 / 9 is the division of two integers, and as such uses integer division, which performs the division normally and then truncates the result. So the result of 5 / 9 is always the integer 0.
Try:
double celsiusTemp = (5.0 / 9) * (tempDouble - 32);
If you evaulate (5/9) as an integer, then it is just 0.