Labrador/Desktop_Interface/ui_elements/siprint.cpp

60 lines
1.4 KiB
C++
Raw Normal View History

#include "siprint.h"
2021-06-24 11:12:28 +01:00
siprint::siprint(const char *unitsInit, double valInit)
2021-06-24 11:32:34 +01:00
: value(valInit)
{
strncpy(units, unitsInit, 6);
}
char* siprint::printVal(){
2021-06-24 11:32:34 +01:00
std::string suffix;
double modifiedValue;
bool negative = (value < 0);
2021-06-24 11:32:34 +01:00
char* tempStringPtr = printString;
if (negative)
{
printString[0] = '-';
tempStringPtr++;
}
2021-06-24 11:32:34 +01:00
if (abs(value) >= 1000000000000000000)
{
sprintf(tempStringPtr, "Inf %s", units);
}
2021-06-24 11:32:34 +01:00
else if (abs(value) >= 1000000)
{
sprintf(tempStringPtr, "%.2fM%s", abs(value)/1000000, units);
}
2021-06-24 11:32:34 +01:00
else if (abs(value) >= 1000)
{
sprintf(tempStringPtr, "%.2fk%s", abs(value)/1000, units);
}
2021-06-24 11:32:34 +01:00
else if (abs(value) >= 1)
{
sprintf(tempStringPtr, "%.2f%s", abs(value), units);
}
2021-06-24 11:32:34 +01:00
else if (abs(value) >= 0.001)
{
sprintf(tempStringPtr, "%.2fm%s", abs(value)*1000, units);
}
2021-06-24 11:32:34 +01:00
else if (abs(value) >= 0.000001)
{
sprintf(tempStringPtr, "%.2fu%s", abs(value)*1000000, units);
}
2021-06-24 11:32:34 +01:00
else if (abs(value) >= 0.000000001)
{
sprintf(tempStringPtr, "%.2fn%s", abs(value)*1000000000, units);
}
2021-06-24 11:32:34 +01:00
else if (abs(value) >= 0.000000000001)
{
sprintf(tempStringPtr, "%.2fp%s", abs(value)*1000000000000, units);
}
2021-06-24 11:32:34 +01:00
else if (abs(value) >= 1)
{
sprintf(tempStringPtr, "%.2f%s", abs(value), units);
}
return printString;
}