i making app read battery percentage using swift! right out this: 61.0% or 24.0% or 89.0% i'm trying fix getting rid of .0 it's int. code far:
import uikit class viewcontroller: uiviewcontroller { @iboutlet weak var infolabel: uilabel! var batterylevel: float { return uidevice.current.batterylevel } var timer = timer() func scheduledtimerwithtimeinterval(){ timer = timer.scheduledtimer(timeinterval: 60, target: self, selector: #selector(self.somefunction), userinfo: nil, repeats: true) } func somefunction() { self.infolabel.text = "\(batterylevel * 100)%" } override func viewdidload() { super.viewdidload() uidevice.current.isbatterymonitoringenabled = true somefunction() scheduledtimerwithtimeinterval() } override func didreceivememorywarning() { super.didreceivememorywarning() } }
i have tried this:
var realbatterylevel = int(batterylevel)
however, error
i have tried other method none luck. please, solutions awesome! in advance!
edit
i considering making float batterylevel string , replacing ".0" "" , have seen somewhere, however, i'm not sure how!
try this:
func somefunction() { self.infolabel.text = string(format: "%.0f%%", batterylevel * 100) }
for future reference, complete string format specifiers listed here.
Comments
Post a Comment