Grisu.net
Grisu is a fast, new (2010) algorithm for converting a floating-point number to a decimal string. It was introduced in a paper by Florian Loitsch (http://dl.acm.org/citation.cfm?doid=1809028.1806623) and the author claims it to be as much as 4 times faster than previous techniques. The code here is a port of the C++ code in the "double-conversion" project on Google Code (http://code.google.com/p/double-conversion/) to C#.
Install / Use
/learn @kring/Grisu.netREADME
Grisu is a fast, new algorithm for converting a floating-point number to a decimal string. It was introduced in a paper by Florian Loitsch in 2010 (http://dl.acm.org/citation.cfm?doid=1809028.1806623) and it as much as four times faster than previous techniques.
The code here is a port of the C++ code in the "double-conversion" project on Google Code (http://code.google.com/p/double-conversion/) to C#. My focus is on very fast JSON serialization of floating point numbers, however, so I eliminated all the options and configurability.
Instead of offering options, my port aims to:
- Guarantee that the generated strings can be parsed back to an identical double. It uses the fewest digits possible while still achieving this.
- Produce the most compact JSON-compatible strings possible by choosing regular decimal or scientific notation, whichever is shorter.
- Be really, really fast.
Using this library is simple, because it really has only one method.
StringBuilder builder = new StringBuilder();
GrisuDotNet.Grisu.DoubleToString(1.23, builder);
Assert.AreEqual("1.23", builder.ToString());
In my tests on a 64-bit version of .NET 4, this library writes doubles to a StringBuilder over two times faster than StringBuilder's default serialization of doubles, while guaranteeing that the strings round-trip back to the same doubles. When the "R" format specifier is used with Double.ToString to guarantee that a double round-trips, this library produces output over four times faster and also generates more compact output.
If you make any improvements to this code - especially performance improvements - please send me a pull request.
Related Skills
node-connect
348.2kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
108.9kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
348.2kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
348.2kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
