Number format not working correctly

Post your questions and help other users.

Moderator: Martin

Post Reply
BCS guy
Posts: 3
Joined: 01 Jan 2017 14:38

Number format not working correctly

Post by BCS guy » 27 Feb 2017 04:48

So I am using a battery widget someone shared here. I have made a few modifications so I could get the widget on my smart watch too, and the battery life didn't work correct, so I swapped it to voltage all the time. The issue I am having is that I wanted to convert the temperature from Celsius to Fahrenheit. Here are the instructions for the operations.

global_battery_level=battery_level;
global_battery_health=battery_health;
global_battery_percentage=battery_percentage;
global_battery_plugged=battery_plugged;
global_battery_present=battery_present;
global_battery_scale=battery_scale;
global_battery_status_text = "";
if (battery_status == 4){
global_battery_status_text = "discharging";
}
if (battery_status == 2) {
global_battery_status_text = "charging";
}
if (battery_status == 5) {
global_battery_status_text = "full";
}
global_battery_technology=battery_technology;
global_battery_voltage=battery_voltage;
global_battery_temperature="{battery_temperature*1.8+32,numberformat,0.00}";
global_battery_temperature=global_battery_temperature/10;

global_battCurrentTime = getDate();
global_battTimeRemain = 0;
battDiff = global_battChargedLevel - global_battery_level - 5;
battTimeDiff = global_battCurrentTime - global_battChargedTime;
if (battDiff > 0){
if (battTimeDiff > 0) {
global_battTimeRemain = global_battery_level * (battTimeDiff / battDiff);
global_battRemainText = "{global_battTimeRemain, dateformat, kk}h {global_battTimeRemain, dateformat, mm}m";
}}

if (global_battTimeRemain > 86400000){
days = global_battTimeRemain / 86400000;
global_battRemainText = "{days}d+";
}

And

//Force update of widget text items
setWidgetElementProperty("BatteryBDIV1", "Level", "text", "{global_battery_level}%");
setWidgetElementProperty("BatteryBDIV1", "STATUS", "text", "{global_battery_status_text}");
setWidgetElementProperty("BatteryBDIV1", "Voltage", "text", "{global_battery_voltage}mV");
setWidgetElementProperty("BatteryBDIV1", "Temp", "text", "{global_battery_temperature}°F");
setWidgetElementProperty("BatteryBDIV1", "Ellipse_fill", "outlinecolor", "#ff00ff00");

arcind = 3.6*global_battery_level;
setWidgetElementProperty("BatteryBDIV1", "Ellipse_fill", "sweepangle", arcind);

/*if (global_battTimeRemain > 0) {
setWidgetElementProperty("BatteryBDIV1", "Voltage", "text", "{global_battRemainText}");
}
else {
setWidgetElementProperty("BatteryBDIV1", "Voltage", "text", "...");
} */

The problem is even with the number format in there the widget will show an extended value part the decimal point.

Thanks

User avatar
Scotty
Posts: 78
Joined: 26 Aug 2016 20:29
Location: Southern California

Re: Number format not working correctly

Post by Scotty » 27 Feb 2017 15:51

You wrote: The problem is even with the number format in there the widget will show an extended value part the decimal point.

Did you mean "past the decimal point"?

If so - if you want to show the number as an integer (with rounding to the nearest integer, e.g. 25.65 would be displayed as 26) - you're using an incorrect numberformat. It should be numberformat,0 (not numberformat,0.00)

BCS guy
Posts: 3
Joined: 01 Jan 2017 14:38

Re: Number format not working correctly

Post by BCS guy » 02 Mar 2017 19:10

Scotty wrote:You wrote: The problem is even with the number format in there the widget will show an extended value part the decimal point.

Did you mean "past the decimal point"?

If so - if you want to show the number as an integer (with rounding to the nearest integer, e.g. 25.65 would be displayed as 26) - you're using an incorrect numberformat. It should be numberformat,0 (not numberformat,0.00)
Yes I should have said past the decimal point. I was writing that from my phone in bed, I apologize. The value shown on the widget would be 60.2100000000001, and I would like it to be just 60.2. Now I don't know if I was having it calculate properly, I was attempting to use the formula {battery_temperature*1.8+32,numberformat,0.00} but I never did check the math.

User avatar
Scotty
Posts: 78
Joined: 26 Aug 2016 20:29
Location: Southern California

Re: Number format not working correctly

Post by Scotty » 02 Mar 2017 19:41

I just tested your formula, *1.8+32,numberformat,0.00, in my battery flow, and it correctly converted C to F, and showed the value to the second decimal place (80.06F)

Let's go back to your original post, & see if we can fix the problem with the display on your watch. You said that it was showing "an extended value past the decimal point", but you didn't indicate how many decimal places were being displayed. The script entry numberformat,0.00 should have limited this to 2 decimal places, but now that I know (from your more recent post) that it's displaying many more than two, the problem takes on a different complexion.

The order in which you enter math-related lines in a script makes a difference. The numberformat function doesn't actually change the numerical value of the variable - it just DISPLAYS the variable at the desired number of decimal places. So, the variable retains whatever number of decimal places it had to start with, and if you then do math on the variable, and display the result (without repeating the numberformat), you'll probably display something with multiple decimal places. So, you should apply numberformat only at the end - AFTER you've completed all of the calculations.

So, from your first post, the 2 relevant lines in your script are:

global_battery_temperature="{battery_temperature*1.8+32,numberformat,0.00}";
global_battery_temperature=global_battery_temperature/10;

I suggest that you replace those 2 lines with the 2 lines below, and see what happens:

global_battery_temperature=battery_temperature/10;
global_battery_temperature="{global_battery_temperature*1.8+32,numberformat,0.0}";

The differences are: (i) swapped the order of the lines (with required change to the variable names) & (ii) changed numberformat to give 1 decimal place (which, from your most recent post, appears to be what you want to be displayed on your watch).

Good luck!

BCS guy
Posts: 3
Joined: 01 Jan 2017 14:38

Re: Number format not working correctly

Post by BCS guy » 07 Mar 2017 20:30

Awesome, works on both phone and watch. Thank you

Post Reply