Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 449
David_D Avatar asked Aug 22 '13 14:08

David_D


1 Answers

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.

like image 187
M.Bennett Avatar answered Oct 06 '22 15:10

M.Bennett