Format double in decimal notation without trailing zeros after the decimal point
Baz via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Mar 28 20:29:25 PDT 2015
On Friday, 27 March 2015 at 15:02:19 UTC, akaDemik wrote:
> The task seemed very simple. But I'm stuck.
> I want to:
> 1234567890123.0 to "1234567890123"
> 1.23 to "1.23"
> 1.234567 to "1.2346".
> With format string "%.4f" i get "1.2300" for 1.23.
> With "%g" i get "1.23456789e+12" for "1234567890123.0".
> I can not believe that it is not implemented. What did I miss?
such a format specifier does not exist.
[.number] means the minimal digits to display, so there is always
at least `number` digits.
In your three examples, there is no common way to format them,
you have to write you own helper:
----
struct YourExoticFormater
{
private float _value;
alias _value this;
string toString()
{
// here you test the number and you choose how to diplay it.
// for example if frac() returns 0 you return the string
repr
// esentation of the the integral part, etc...
// this will work with to!string(), probably format %s (?),
and the
// write() functions family.
}
}
----
More information about the Digitalmars-d-learn
mailing list