Search code examples
androidandroid-intentbatterytemperature

How get battery temperature with decimal?


How can i get the battery temperature with decimal? Actually i can calculate it with

int temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,0);

But in this way the result will be for example 36 °C.. I want something that show me 36.4 °C How can i do?


Solution

  • Google says here :

    Extra for ACTION_BATTERY_CHANGED: integer containing the current battery temperature.

    The returned value is an int representing, for example, 27.5 Degrees Celcius as "275" , so it is accurate to a tenth of a centigrade. Simply cast this to a float and divide by 10.

    Using your example:

    int temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,0);
    float tempTwo = ((float) temp) / 10;
    

    OR

    float temp = ((float) intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,0) / 10;
    

    You don't need to worry about the 10 as an int since only one operand needs to be a float for the result to be one too.