How to get battery temperature with decimal?

How can I get the battery temperature with decimal? In fact, I can calculate it using

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

But in this way the result will be, for example, 36 °C I need something that shows me 36.4 °C How can I do this?

+4
source share
3 answers

Google says here :

Additionally for ACTION_BATTERY_CHANGED: an integer containing the current battery temperature.

The return value is an int, representing, for example, 27.5 degrees Celcius as "275", so it is equal to a tenth of a second. Just drop it onto the 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 do not need to worry about 10 as an int, because only one operand must be a float in order for the result to be as well.

+13
source

This is the only way I know that you can get the temperature of the battery, and is always int.

According to the documentation:

public static final String EXTRA_TEMPERATURE

Additionally for ACTION_BATTERY_CHANGED: an integer containing the current battery temperature.

But you can divide by 10.0f to get one decimal number.

float ftemp = temp / 10.0f;

+1
source
  public static String batteryTemperature(Context context) { Intent intent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); float temp = ((float) intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE,0)) / 10; return String.valueOf(temp) + "*C"; } 
+1
source

Source: https://habr.com/ru/post/1498407/


All Articles