summaryrefslogtreecommitdiff
path: root/source
diff options
context:
space:
mode:
authorEugen Wissner <belka@caraus.de>2017-11-25 14:59:27 +0100
committerEugen Wissner <belka@caraus.de>2017-11-25 14:59:27 +0100
commitfbf6ec5250143ed543c537a0b0fe288542c5610f (patch)
tree48f2a0096cf1f3f3c25fba35065de92d5183a0ee /source
parentac317aa9d62792e24feb3f094aa427935e3addf8 (diff)
downloadtanya-fbf6ec5250143ed543c537a0b0fe288542c5610f.tar.gz
format: Check if the scientific form is to be used
Diffstat (limited to 'source')
-rw-r--r--source/tanya/format/package.d63
1 files changed, 60 insertions, 3 deletions
diff --git a/source/tanya/format/package.d b/source/tanya/format/package.d
index 2343d2f..9cfc803 100644
--- a/source/tanya/format/package.d
+++ b/source/tanya/format/package.d
@@ -501,6 +501,19 @@ Zeroed:
return result[0 .. length];
}
+private void leadSign(bool negative, ref char[8] sign)
+{
+ if (negative)
+ {
+ sign[0] = 1;
+ sign[1] = '-';
+ }
+ else
+ {
+ sign[0] = 0;
+ }
+}
+
package(tanya) String format(string fmt, Args...)(Args args)
{
String ret;
@@ -535,12 +548,56 @@ package(tanya) String format(string fmt, Args...)(Args args)
{
int precision = 6;
bool negative;
- int decimalPos;
- uint l;
+ int decimalPoint;
char[512] num;
+ char[8] tail;
+ char[8] lead;
// Read the double into a string
- const(char)[] sn = real2String(args[0], num, decimalPos, negative);
+ const(char)[] sn = real2String(args[0], num, decimalPoint, negative);
+
+ uint n = precision;
+
+ // Clamp the precision and delete extra zeros after clamp.
+ if (sn.length > (cast(uint) precision))
+ {
+ sn = sn[0 .. precision];
+ }
+ while (sn.length > 1 && precision != 0 && sn[$ - 1] == '0')
+ {
+ --precision;
+ sn = sn[0 .. $ - 1];
+ }
+
+ // Should the scientific notation be used?
+ if (decimalPoint <= -4 || decimalPoint > (cast(int) n))
+ {
+ if (precision > (cast(int) sn.length))
+ {
+ precision = cast(int) (sn.length - 1);
+ }
+ else if (precision != 0)
+ {
+ --precision; // There is 1 digit before the decimal point.
+ }
+ }
+ else
+ {
+ if (decimalPoint > 0)
+ {
+ precision = decimalPoint < (cast(int) sn.length)
+ ? cast(int) (sn.length - decimalPoint)
+ : 0;
+ }
+ else if (precision > (cast(int) sn.length))
+ {
+ precision = -decimalPoint + cast(int) sn.length;
+ }
+ else
+ {
+ precision = -decimalPoint + precision;
+ }
+ }
}
else
{