Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
February 24, 2015 Float to string with more digits? | ||||
---|---|---|---|---|
| ||||
Is there a more accurate way to do a float and or double to string than... to!string(float); As that seems to limit itself to 6 digits. |
February 24, 2015 Re: Float to string with more digits? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Almighty Bob | On Tue, 24 Feb 2015 20:04:04 +0000, Almighty Bob wrote:
> Is there a more accurate way to do a float and or double to string than...
>
> to!string(float);
>
> As that seems to limit itself to 6 digits.
Use std.string.format or std.format.formattedWrite. std.format contains a description of the various format specifiers. You'll probably want something like "%.12f", which formats a floating point number with 12 digits of precision.
|
February 24, 2015 Re: Float to string with more digits? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Almighty Bob | On Tue, Feb 24, 2015 at 08:04:04PM +0000, Almighty Bob via Digitalmars-d-learn wrote: > Is there a more accurate way to do a float and or double to string than... > > to!string(float); > > As that seems to limit itself to 6 digits. What about std.format.format("%.12f", myFloat)? Or, if you like: string floatToString(F)(F f) if (isFloatingPoint!F) { // Extract as many digits as are available for this // type. return std.format.format("%.*f", F.dig, f); } T -- If you want to solve a problem, you need to address its root cause, not just its symptoms. Otherwise it's like treating cancer with Tylenol... |
February 24, 2015 Re: Float to string with more digits? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Justin Whear | On Tuesday, 24 February 2015 at 20:08:55 UTC, Justin Whear wrote:
> On Tue, 24 Feb 2015 20:04:04 +0000, Almighty Bob wrote:
>
>> Is there a more accurate way to do a float and or double to string
>> than...
>>
>> to!string(float);
>>
>> As that seems to limit itself to 6 digits.
>
> Use std.string.format or std.format.formattedWrite. std.format contains
> a description of the various format specifiers. You'll probably want
> something like "%.12f", which formats a floating point number with 12
> digits of precision.
that did it, thanks,
:)
|
February 24, 2015 Re: Float to string with more digits? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Almighty Bob | Just to clarify what i needed was... %.8g or %.7e Significant digits, or fixed width scientific form. I needed more significant digits. %.8f controls how many digits to print after the decimal point, which is not the same thing. |
Copyright © 1999-2021 by the D Language Foundation