-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathenhancedFormat.js
More file actions
85 lines (75 loc) · 2.16 KB
/
enhancedFormat.js
File metadata and controls
85 lines (75 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//// For GuiMods GUI
//
// formats values to varying resolution depending on the value
// the global variable sys.kilowattThreshold specifies the transition between units an K units
// item is a VBusItem
// unit is an optional string ("W", "A", "V", etc appended to the value
// if unit is omitted, VBusItem unit is used
// to disable the VBusItem unit specify "", or some other value
// functions that take a value can be used when a VBusItem doesn't provide the value
// for value > 1000, only K will always be displayed even if the unit is blank
// the optional precision parameter always displays the value to the indicated number of decimal point
// (e.g, 2 would display 12.34)
// scaling to kilo values is disabled
// you must specify a unit if precision is needed !!!
// the ...Abs versions always display a postive number
// used when a direction indicator is also displayed
function formatValue (value, unit, precision)
{
if (unit == undefined)
unit = ""
var threshold
if (sys.kilowattThreshold == undefined)
threshold = 1000
else
threshold = sys.kilowattThreshold
if (threshold == 0 || (value >= 0 && value < threshold) || (value < 0 && value > -threshold))
{
if (precision != undefined)
return value.toFixed (precision) + " " + unit
else if (value >= 100 || value <= -100)
return value.toFixed (0) + " " + unit
else
return value.toFixed (1) + " " + unit
}
else
{
if (precision != undefined)
return value.toFixed (precision) + " " + unit
else if (value >= 10000 || value <= -10000)
return (value/1000).toFixed (1) + " K" + unit
else
return (value/1000).toFixed (2) + " K" + unit
}
}
function formatValueAbs (value, unit, precision)
{
if (unit == undefined)
unit = ""
if ( value < 0)
value = -value
return formatValue (value, unit, precision)
}
function formatVBusItem (item, unit, precision)
{
if (item.valid)
{
if (unit == undefined)
unit = item.unit
return formatValue (item.value, unit, precision)
}
else
return ""
}
function formatVBusItemAbs (item, unit, precision)
{
var value
if (item.valid)
{
if (unit == undefined)
unit = item.unit
return formatValueAbs (item.value, unit, precision)
}
else
return ""
}