I thought this would be fairly straightforward, but I can't seem to work it out or find an existing question that's covers my situation.
我认为这将是相当简单的,但我似乎无法解决或找到一个涵盖我的情况的现有问题。
Originally I set up a number of variables as follows:
最初我设置了一些变量如下:
var score :Int! = 0
var totalquestionsasked :Int! = 0
var percentagecorrect :Int! = 0
This got all my values calculating okay, but I ran into problems with the percentage.
这让我所有的价值都计算好了,但是我遇到了百分比的问题。
My basic formula for the percentage is:
我的百分比基本公式是:
percentagecorrect = score / totalquestionsasked * 100
However, I couldn't get this to display when using the value in a text label.
但是,在文本标签中使用值时,无法显示此内容。
So after some research, I changed my variables to this instead:
所以经过一些研究后,我改变了我的变量:
var score :Double = 0.0
var totalquestionsasked :Double = 0.0
var percentagecorrect :Double = 0.0
And still used the same formula for the percentage:
并且仍然使用相同的百分比公式:
percentagecorrect = score / totalquestionsasked * 100
However, while this displayed okay, I didn't want all the decimal places. So after some more research I came across an extension in another question and adapted it for my application. My extension is:
然而,虽然这显示没问题,但我不想要所有小数位。经过一些研究后,我在另一个问题中遇到了一个扩展,并根据我的应用进行了调整。我的扩展是:
extension Double {
var roundTo0f: Double {return Double(round(1*self)/1) }
var roundTo1f: Double {return Double(round(10*self)/10) }
var roundTo2f: Double {return Double(round(100*self)/100) }
var roundTo3f: Double {return Double(round(1000*self)/1000) }
}
And I've tried using the extension as follows:
我尝试使用扩展如下:
percentagecorrect = score / totalquestionsasked * 100
let percentagevalue: Double = percentagecorrect.roundTo0f
labelPercent.text = "\(percentagevalue)"
The end result is that this is pretty close to what I want. However it does still display the percentage to 1 decimal place. (As an aside, it's not accurate - what I mean is that if the score is 1 and the number of questions asked is 3, the above code displays the percentage as 33.0 instead of 33.3, but I digress)
最终的结果是,这非常接近我想要的。但它仍然显示百分比到1位小数。 (顺便说一句,它不准确 - 我的意思是,如果得分为1且问题的数量为3,则上面的代码显示百分比为33.0而不是33.3,但我离题了)
What I'm actually wanting is the value to round up or down to the nearest whole number before displaying the value. In other words, what I want is
我真正想要的是在显示值之前向上或向下舍入到最接近的整数的值。换句话说,我想要的是
- if the value is 33.3 for it to round down to 33 and display as 33
- 如果值为33.3,则向下舍入为33并显示为33
- if the value is 66.6 for it to round up to 67 and display as 67
- 如果值为66.6,则向上舍入为67并显示为67
That is I want no decimal places at all in the displayed result. At present, it rounds up/down correctly but sill displays ".0" at the end. If this isn't possible, then I want it to display to one decimal place correctly.
那就是我想在显示的结果中根本没有小数位。目前,它正确地向上/向下舍入,但最后显示“.0”。如果这是不可能的,那么我希望它正确显示到一个小数位。
I am new to programming and Swift is the first language I'm trying to learn, so maybe this is just a newbie mistake, but I really can't find what I'm doing wrong.
我是编程新手,Swift是我想学的第一种语言,所以也许这只是一个新手的错误,但我真的找不到我做错了什么。
How do I get this to work?
我如何让它工作?
5 个解决方案
#1
10
You can use the built in round function and cast to an Int.
您可以使用内置的round函数并转换为Int。
print(Int(round(33.3)))
print(Int(round(66.6)))
If you want an extension method to return a string, you can use this:
如果您希望扩展方法返回一个字符串,您可以使用:
extension Double {
func roundTo(decimalPlaces: Int) -> String {
return NSString(format: "%.\(decimalPlaces)f", self) as String
}
}
#2
1
You can use the format specifier of NSString and add it to your Double extension as functions.
您可以使用NSString的格式说明符,并将其作为函数添加到Double扩展中。
extension Double
{
func roundTo0f() -> NSString
{
return NSString(format: "%.0f", self)
}
func roundTo1f() -> NSString
{
return NSString(format: "%.1f", self)
}
func roundTo2f() -> NSString
{
return NSString(format: "%.2f", self)
}
func roundToNf(n : Int) -> NSString
{
return NSString(format: "%.\(n)f", self)
}
}
let d : Double = 9.12
print(d.roundTo0f())
print(d.roundTo1f())
print(d.roundTo2f())
#3
1
In pure integer arithmetic,
在纯整数运算中,
var score : Int = ...
var totalquestionsasked : Int = ...
let percentagecorrect = (score * 200 + totalquestionsasked)/(2 * totalquestionsasked)
would do the trick.
会做的伎俩。
How does this work? Computing the percentage as
这个怎么用?将百分比计算为
(score * 100)/totalquestionsasked
truncates the result of the division towards zero. For the desired rounding behavior, we would have to compute
将除法的结果截断为零。对于期望的舍入行为,我们必须进行计算
(score * 100)/totalquestionsasked + 1/2
in floating point arithmetic and then truncate the result to an integer. Combining terms give the expression
在浮点运算中,然后将结果截断为整数。结合术语给出了表达
(score * 200 + totalquestionsasked)/(2 * totalquestionsasked)
which, when computed in integer arithmetic, gives the correct result.
当以整数运算计算时,它给出正确的结果。
#4
0
You can do the round off yourself. I am assuming a one for one answer score valuation. I've plugged in some numbers so you can paste this into a playground. You can change to value in "if left" to anything you want. .51 etc. Just because the IRS has you round off at 50 cents that doesn't mean you have to.
你可以自己做完。我假设一对一的答案评分。我插了一些数字,所以你可以将它粘贴到游乐场。您可以将“if left”中的值更改为您想要的任何值。 .51等等。仅仅因为美国国税局让你以50美分的价格完成,这并不意味着你必须这样做。
var score :Double! = 359
var totalquestionsasked :Double! = 400
var percentagecorrect :Double! = (score / totalquestionsasked) * 100
var left = percentagecorrect - Double(Int(percentagecorrect))
if left >= 0.50 {
percentagecorrect = percentagecorrect + 1.0
}
var answer = Int(percentagecorrect)
#5
0
You can use this extension:
您可以使用此扩展程序:
extension Double {
/// Rounds the double to decimal places value
func roundToPlaces(places:Int) -> Double {
let divisor = pow(10.0, Double(places))
return round(self * divisor) / divisor
}
}
#1
10
You can use the built in round function and cast to an Int.
您可以使用内置的round函数并转换为Int。
print(Int(round(33.3)))
print(Int(round(66.6)))
If you want an extension method to return a string, you can use this:
如果您希望扩展方法返回一个字符串,您可以使用:
extension Double {
func roundTo(decimalPlaces: Int) -> String {
return NSString(format: "%.\(decimalPlaces)f", self) as String
}
}
#2
1
You can use the format specifier of NSString and add it to your Double extension as functions.
您可以使用NSString的格式说明符,并将其作为函数添加到Double扩展中。
extension Double
{
func roundTo0f() -> NSString
{
return NSString(format: "%.0f", self)
}
func roundTo1f() -> NSString
{
return NSString(format: "%.1f", self)
}
func roundTo2f() -> NSString
{
return NSString(format: "%.2f", self)
}
func roundToNf(n : Int) -> NSString
{
return NSString(format: "%.\(n)f", self)
}
}
let d : Double = 9.12
print(d.roundTo0f())
print(d.roundTo1f())
print(d.roundTo2f())
#3
1
In pure integer arithmetic,
在纯整数运算中,
var score : Int = ...
var totalquestionsasked : Int = ...
let percentagecorrect = (score * 200 + totalquestionsasked)/(2 * totalquestionsasked)
would do the trick.
会做的伎俩。
How does this work? Computing the percentage as
这个怎么用?将百分比计算为
(score * 100)/totalquestionsasked
truncates the result of the division towards zero. For the desired rounding behavior, we would have to compute
将除法的结果截断为零。对于期望的舍入行为,我们必须进行计算
(score * 100)/totalquestionsasked + 1/2
in floating point arithmetic and then truncate the result to an integer. Combining terms give the expression
在浮点运算中,然后将结果截断为整数。结合术语给出了表达
(score * 200 + totalquestionsasked)/(2 * totalquestionsasked)
which, when computed in integer arithmetic, gives the correct result.
当以整数运算计算时,它给出正确的结果。
#4
0
You can do the round off yourself. I am assuming a one for one answer score valuation. I've plugged in some numbers so you can paste this into a playground. You can change to value in "if left" to anything you want. .51 etc. Just because the IRS has you round off at 50 cents that doesn't mean you have to.
你可以自己做完。我假设一对一的答案评分。我插了一些数字,所以你可以将它粘贴到游乐场。您可以将“if left”中的值更改为您想要的任何值。 .51等等。仅仅因为美国国税局让你以50美分的价格完成,这并不意味着你必须这样做。
var score :Double! = 359
var totalquestionsasked :Double! = 400
var percentagecorrect :Double! = (score / totalquestionsasked) * 100
var left = percentagecorrect - Double(Int(percentagecorrect))
if left >= 0.50 {
percentagecorrect = percentagecorrect + 1.0
}
var answer = Int(percentagecorrect)
#5
0
You can use this extension:
您可以使用此扩展程序:
extension Double {
/// Rounds the double to decimal places value
func roundToPlaces(places:Int) -> Double {
let divisor = pow(10.0, Double(places))
return round(self * divisor) / divisor
}
}