On a related note, a customer just asked about formatting a float into scientific notation in PAC Display.
I told him about using [B]NumberToString[/B] in his logic, then displaying the resulting string, as we discussed above.
He specifically asked for the format 1.23E-08 but [B]NumberToString [/B]gives you: 1.234567e-08. (With a four more places after the decimal point, and a capital E â which might be okay, Iâve not yet heard back from him.)
In the meantime, for grins and giggles, I pondered how to adjust for that w/a couple of OptoScript lines ALMOST work perfectly:
NumberToString(fGauge, sGauge);
// drop the 4 extra characters and convert the lower-case e to upper case E
// "1.333000e+03"
GetSubstring(sGauge, 0, 4, sSignificand);
GetSubstring(sGauge, 9, 3, sExponent);
// put the piece together and replace that e with an E
sGaugeHMI = sSignificand + "E" + sExponent;
I say [I]almost [/I]because⌠and I could be thinking too hard about this but⌠do you see why it might work for 1.33E+03 but not 0.6666666 ?
That method effectively truncates at the least significant digit. Which might also be okay for him, given the specific numbers he sent me.
But just in case, I came up with a few more lines of OptoScript, but itâs getting kind of ugly. Wondering if anyone else out there in OptoLand as a more elegant way of formatting a float value to a specific number of significant digits in that significand/mantissa for scientific notation?
Hereâs the longer version which does a Round at the 1/100ths place:
NumberToString(fGauge, sGauge);
// That gives us a string in this format: "1.333000e+03"
// but we only want 2 sig digits, so let's convert the e to a space so we use the StringToFloat and Round comamnds
sGauge[8] = ' '; // SetNthCharacter works too
fGaugeX100 = StringToFloat(sGauge) * 100; // now we should have a value like 133.3
fGaugeX100 = Round( fGaugeX100 );
NumberToString(fGaugeX100, sGaugeX100);
// drop the 4 extra characters and convert the lower-case e to upper case E
// "1.333000e+03"
GetSubstring(sGaugeX100, 0, 4, sSignificand);
GetSubstring(sGauge, 9, 3, sExponent);
// put the piece together and replace that e with an E
sGaugeHMI = sSignificand + "E" + sExponent;
Clear as mud? Other ideas/suggestions?