how to convert this result of calculation flutter - flutter

wanna know how to show this result like int number
var symbol = data[index]["symbol"];
double cureentprice = datarates["price"]["$symbol"];////this double is 1.17150
double enrty = double.parse(data[index]["entryprice"]);////this is 1.17100
double lo = cureentprice - enrty ;//i got like this result 0.00050
as you see above i got the result 0.00050 but i need it like this 50
any idea to do somthing like that??

Try This. Maybe This should Help. If this doesn't work then let me know.
var symbol = data[index]["symbol"];
double cureentprice = datarates["price"]["$symbol"];////this double is 1.17150
double enrty = double.parse(data[index]["entryprice"]);////this is 1.17100
// double lo = cureentprice - enrty ;//i got like this result 0.00050
int scale = 100000;
double lo = currentPrice - enrty;
var value1 = (lo * scale).floor();
var value2 = (lo * scale).ceil();
print(value1);
print(value2);

You can first scale and then round-off using floor() or ceil() function to get desired output.
double currentPrice = 1.17150;
double enrty = 1.17100;
int scale = 100000;
double lo = currentPrice - enrty;
print((lo * scale).floor()); //prints 49
print((lo * scale).ceil()); //prints 50

Related

Odd division result in swift [duplicate]

I'm trying to make a math app with different equations and formulas but I'm trying to circle sector but i just wanted to try to divide the input value by 360 but when I do that it only says 0 unless the value is over 360. I have tried using String, Double and Float with no luck I don't know what I'm doing is wrong but down here is the code. I'm thankful for help but I have been sitting a while and searched online for an answer with no result I might have been searching with the wrong search.
if graderna.text == ""{
}
else{
var myInt: Int? = Int(graderna.text!) // conversion of string to Int
var myInt2: Int? = Int(radien.text!)
let pi = 3.1415926
let lutning = 360
let result = (Double(myInt! / lutning) * Double(pi))
svar2.text = "\(result)"
}
Your code is performing integer division, taking the integer result and converting it to a double. Instead, you want to convert these individual integers to doubles and then do the division. So, instead of
let result = (Double(myInt! / lutning) * Double(pi))
You should
let result = Double(myInt!) / Double(lutning) * Double(pi)
Note, Double already has a .pi constant, so you can remove your pi constant, and simplify the above to:
let result = Double(myInt!) / Double(lutning) * .pi
Personally, I’d define myInt and lutning to be Double from the get go (and, while we’re at it, remove all of the forced unwrapping (with the !) of the optionals):
guard
let text = graderna.text,
let text2 = radien.text,
let value = Double(text),
let value2 = Double(text2)
else {
return
}
let lutning: Double = 360
let result = value / lutning * .pi
Or, you can use flatMap to safely unwrap those optional strings:
guard
let value = graderna.text.flatMap({ Double($0) }),
let value2 = radien.text.flatMap({ Double($0) })
else {
return
}
let lutning: Double = 360
let result = value / lutning * .pi
(By the way, if you’re converting between radians and degrees, it should be 2π/360, not π/360.)
You are dividing an Int by an Int.
Integer division rounds to the nearest integer towards zero. Therefore for example 359 / 360 is not a number close to 1, it is 0. 360 / 360 up to 719 / 360 equals 1. 720 / 360 to 1079 / 360 equals 2, and so on.
But your use of optionals is atrocious. I'd write
let myInt = Int(graderna.text!)
let myInt2 = Int(radien.text!)
if let realInt = myInt, realInt2 = myInt2 {
let pi = 3.1415926
let lutning = 360.0
let result = Double (realInt) * (pi / lutning)
svar2.text = "\(result)"
}
In the line let result = (Double(myInt! / lutning) * Double(pi)) you cast your type to double after dividing two integers so your result will always be zero. You have to make them doubles before division.
let result = (Double(myInt!) / Double(lutning)) * Double(pi))
If you want the value should be correct, then try as
let division = ((Float(V1) / Float(V2)) * Float(pi))

Swift 3 - To the power of (pow) function not working as expected

Please could somebody help me. I am trying to run a simple compounding calculation in Swift.
Formula I am trying to recreate:
T = P(1+r/n)^(n*t), where
T = Total, P = Starting amount, r = interest rate, n = number of times compounded and t = number of years
My code as follows:
import Darwin
var total: Double
var startingAmount: Double = 5000.00
var interestRate: Double = 0.05
var numberOfTimesCompounded: Double = 4.0
var numberOfYears: Double = 2.0
var totalYear1: Double
var toThePowerOf: Double
totalYear1 = startingAmount * (1 + interestRate / numberOfTimesCompounded)
toThePowerOf = numberOfTimesCompounded * number of years
total = pow(totalYear1,toThePowerOf)
The answer to the formula should be 5,522.43
In my code above, TotalYear1 = 5062.50 (which is correct) and toThePowerOf = 8.0 (which is correct) However, total shows = 4314398832739892000000.00 which clearly isn't right. Could anyone tell me what I am doing wrong with my calculation of total?
Many thanks
You've actually implemented T = (P(1+r/n))^(n*t), which doesn't even make dimensional sense.
startingAmount needs to be multiplied at the end, it can't be part of the pow:
totalYear1 = (1 + interestRate / numberOfTimesCompounded)
toThePowerOf = numberOfTimesCompounded * number of years // [sic]
total = startingAmount * pow(totalYear1,toThePowerOf)

What is the swift code to apply Round Up or Down Sum Amount?

I try on Xcode - Playground.
This is my code. Beginner.
========
import UIKit
var num1 : Double = 0.055 // Stock Price
var num2 : Double = 18 // Lots
var num3 : Double = 1000 // Share Per Lots
var sum1 : Double = num1 * num2 * num3 // Gross Share Price
var sum5 : Double = sum1 * (0.03/100) // Clearing Charges // Answer Playground Return is " 0.297 "
My Questions is the "sum5" I want answer round up and display " 0.30 "
It is possible in swift code ?
Thanks.
You can get it this way:
var roundOfSum5 : Double = Double(round(100 * sum5)/100) //0.3
Regarding to that answer
If you need to round to a specific place, then you multply by pow(10.0, numberOfPlaces), round, and then divide by pow(10, numberOfPlaces). In your case the number of places is 2.0:
let numberOfPlaces = 2.0
let multiplier = pow(10.0, numberOfPlaces)
let rounded = round(sum5 * multiplier) / multiplier
print(rounded) // 0.3
If you have a number like sum5 = 0.3465 and you want to round to the third place after the decimal you can use 3.0 for numberOfPlaces and get as result 0.347

Cast an int to a Float in Swift

Im trying to cast the results of a calculation (ShotPercentage) to a Float and present the results in the App as a 89 percent for example. But I am struggling with the type casting any help would be greatly appreciated. Here is my code:
// calculate shot percentage
shotPercentage = makeCounter / totalCounter
shotPercentageLabel.text = "\(shotPercentage)"
You can apply a conversion to your Floatresult like this.
shotPercentage = Int(makeCounter / totalCounter)
var shotPercentageInt: Int
shotPercentageInt = 3/5
println("\(shotPercentageInt*100)%") // 0% because 3/5 = 0.6 -> Int = 0
//
var shotPercentageFloat: Float
shotPercentageFloat = 3/5
println("\(shotPercentageFloat*100)%") // 60.0% because 3/5 = 0.6 -> Float = 60%
// Convert Float to Int
var shotPercentageFloatToInt: Int
shotPercentageFloatToInt = Int(shotPercentageFloat)
println("\(shotPercentageFloat)") // 0.6
// It's amazing
You might want to use NSNumberFormatter. It does well going to strings and coming from strings. Regardless, it would look like:
let makeCounter = 15
let totalCounter = 20
let shotPercentage:Float = makeCounter / totalCounter
let formatter = NSNumberFormatter()
formatter.numberStyle = .PercentStyle
if let percentString = formatter.stringFromNumber(shotPercentage) {
shotPercentageLabel.text = percentString
}
In this case the label would read 75%, as the formatter will include the percent sign for you.

Java math issue

Can any one tell me how to write this type of code in eclipse propject. basically i want to square root the samv and samvi variables. i am getting syntax error.
int ki, l,;
int samv = (tdrum / k) / l;
int samvi = (tdrum / ki) / l;
int samv2 = (Math.sqrt(samv);
int samv2i = (Math.sqrt(samvi);
Many Error extra commas less brackets
int ki, l;
int samv = (tdrum / k) / l;
int samvi = (tdrum / ki) / l;
int samv2 = (Math.sqrt(samv));
int samv2i = (Math.sqrt(samvi));
Take care of the brackets. And use a double because sqrt() returns no int:
double samv2 = Math.sqrt(samv);
double samv2i = Math.sqrt(samvi);