我无法理解错误是什么?我有一点变数。
decimal Price = 22348 / 100;
我得到的答案是:223,但我应该得到223.48。有什么问题?
答案 0 :(得分:11)
你在做什么是这样的:
decimal = (decimal)(int / int);
所以你要计算整数(得到你得到的结果),然后把结果转换成小数。
相反,您可以将任一操作数转换为小数:
decimal Price = 22348 / 100M;
答案 1 :(得分:1)
将这些数字声明为十进制变量,然后将除法结果放入另一个十进制变量中。
let json: [String: Any] = ["email": "test_mobile@mysite.com"]
let jsonData = try? JSONSerialization.data(withJSONObject: json)
// create post request
let url = URL(string: "http://httpbin.org/post")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
// insert json data to the request
request.httpBody = jsonData
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else {
print(error?.localizedDescription ?? "No data")
return
}
let responseJSON = try? JSONSerialization.jsonObject(with: data, options: [])
if let responseJSON = responseJSON as? [String: Any] {
print(responseJSON)
}
}
task.resume()
这将给出结果 decimal num1 = 22348;
decimal num2 = 100;
decimal Price = (num1 / num2);
Console.WriteLine(Price);