Float to string with more digits?

H. S. Teoh via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Feb 24 12:20:30 PST 2015


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...


More information about the Digitalmars-d-learn mailing list