'Swift 3 Xcode: How to display battery levels as an integer?

I am making an app to read battery percentage using Swift! Right now my out is something like this: 61.0% or 24.0% or 89.0% What I'm trying to fix is getting rid of the .0 so it's an Int. This is my code so 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 something like this:

var realBatteryLevel = Int(batteryLevel)

However, I get this error

I have tried other method but none with any luck. Please, any solutions would be awesome! Thanks in advance!

EDIT

I was considering making the float batteryLevel into a String and then replacing ".0" with "" and I have seen this somewhere, however, I'm not sure how!



Solution 1:[1]

You just need to convert It inside your function :

func someFunction() {
self.infoLabel.text = "\(Int(batteryLevel * 100))%" }

Solution 2:[2]

Alternately, you could create an Int computed property for batteryLevel:

var batteryLevel: Int {
  return Int(round(UIDevice.current.batteryLevel * 100))
}

Note that you might not be able to get the battery level. You should test for that and display a different string:

if UIDevice.current.batteryState == .unknown {
  self.batteryLevelLabel.text = "n/a"
} else {
  self.batteryLevelLabel.text = "\(self.batteryLevel)%"
}

Also note that rather than running a timer to fetch the battery level, you should subscribe to the .UIDeviceBatteryLevelDidChange notification. The "meat" of a view controller that handles all of this might look as follows:

import UIKit

class ViewController: UIViewController {

  @IBOutlet weak var batteryLevelLabel: UILabel!

  ///Holds the notification handler for battery notifications.
  var batteryNotificationHandler: Any?

  ///A computed property that returns the battery level as an int, using rounding.
  var batteryLevel: Int {
    return Int(round(UIDevice.current.batteryLevel * 100))
  }

  ///A function to display the current battery level to a label, 
  ////or the string "n/a" if the battery level can't be determined.
  func showBatteryLevel() {
    if UIDevice.current.batteryState == .unknown {
      self.batteryLevelLabel.text = "n/a"

    } else {
      self.batteryLevelLabel.text = "\(self.batteryLevel)%"
    }
  }

  override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    ///If we have a battery level observer, remove it since we're about to disappear
    if let observer = batteryNotificationHandler {
      NotificationCenter.default.removeObserver(observer: observer)
    }
  }
  override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    showBatteryLevel() //display the battery level once as soon as we appear

    //Create a notifiation handler for .UIDeviceBatteryLevelDidChange 
    //notifications that calls showBatteryLevel()
    batteryNotificationHandler =
      NotificationCenter.default.addObserver(forName: .UIDeviceBatteryLevelDidChange,
                                             object: nil,
                                             queue: nil, using: {
                                              (Notification) in
                                              self.showBatteryLevel()
    })
  }

    override func viewDidLoad() {
      super.viewDidLoad()
      //Tell UIDevice that we want battery level notifications
      UIDevice.current.isBatteryMonitoringEnabled = true
  }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2