Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
March 27, 2015 Format double in decimal notation without trailing zeros after the decimal point | ||||
---|---|---|---|---|
| ||||
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? |
March 27, 2015 Re: Format double in decimal notation without trailing zeros after the decimal point | ||||
---|---|---|---|---|
| ||||
Posted in reply to akaDemik | On 3/27/15 11:02 AM, 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?
I think you are asking for trouble to do this. Floating point is not exact, so for example, if I do
writefln("%.15f", 123456.789123);
I get:
123456.789122999995016
How far do you want to go before you determine there will only be zeros? It could be infinity.
I'd say your best bet is to format to the max level you want, e.g. "%.6f"
Then trim off any trailing zeros (and decimal point if necessary) after conversion to a string.
-Steve
|
March 29, 2015 Re: Format double in decimal notation without trailing zeros after the decimal point | ||||
---|---|---|---|---|
| ||||
Posted in reply to akaDemik | 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.
}
}
----
|
March 30, 2015 Re: Format double in decimal notation without trailing zeros after the decimal point | ||||
---|---|---|---|---|
| ||||
Posted in reply to Baz | Thank you. Actually, I'm doing this: format("%.4f", d).stripRight('0').stripRight('.') (not so elegant, but it works.) But I thinking that do not know much about the format string. On Sunday, 29 March 2015 at 03:29:26 UTC, Baz wrote: > 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. > } > } > ---- |
March 30, 2015 Re: Format double in decimal notation without trailing zeros after the decimal point | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | Thanks for the reply. I remember about the accuracy of floating point numbers. It is encouraging that the "%g" can handle it. format("%.17g", 123456.789123); // == 123456.789123 And we have a flag "#". As mentioned in documentation: > '#' floating Always insert the decimal point and print trailing zeros. With '#' I get: format("%#.17g", 123456.789123); // == 123456.78912300000 So, with the flag '#' "%g" is almost similar to the "%f". But there is no flag repealing '#' if it is enabled by default (in the case of "%f"). I looked deeper into the function format and realized that my question is more related to the implementation of snprintf. If I understand correctly, snprintf does the job, and std.format provides a safe and very convenient wrapper. On Friday, 27 March 2015 at 17:08:07 UTC, Steven Schveighoffer wrote: > On 3/27/15 11:02 AM, 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? > > I think you are asking for trouble to do this. Floating point is not exact, so for example, if I do > > writefln("%.15f", 123456.789123); > > I get: > > 123456.789122999995016 > > How far do you want to go before you determine there will only be zeros? It could be infinity. > > I'd say your best bet is to format to the max level you want, e.g. "%.6f" > > Then trim off any trailing zeros (and decimal point if necessary) after conversion to a string. > > -Steve |
Copyright © 1999-2021 by the D Language Foundation