SoFunction
Updated on 2025-03-08

C# Use Toml as configuration file pitfalls encountered

I've been playing a program these days, and I suddenly saw that C# uses a picture toml file, which is easy to use, intuitive, and indeed simple.

but. . . . . .

The example written on github

TOML to TomlTable

TOML input file:v

EnableDebug = true
[Server]
Timeout = 1m
[Client]
ServerAddress = "http://127.0.0.1:8080"

Code:

var toml = (filename);
("EnableDebug: " + <bool>("EnableDebug"));
("Timeout: " + <TomlTable>("Server").Get<TimeSpan>("Timeout"));
("ServerAddress: " + <TomlTable>("Client").Get<string>("ServerAddress"));

Output:

EnableDebug: True
Timeout: 00:01:00
ServerAddress: http://127.0.0.1:8080

TomlTable is Nett's generic representation of a TomlDocument. It is a hash set based data structure where each key is represented as a string and each value as a TomlObject.

Using the TomlTable representation has the benefit of having TOML metadata - . the Comments - available in the data model.

It's very useful, so I changed the float type parameter test and the curse is here.

("ServerAddress: " + &lt;TomlTable&gt;("Client").Get&lt;float&gt;("floatXXX"));
Read everything is OK,
What's next?Modify?So I saw thereUpdatefunction
&lt;TomlTable&gt;("Server").Update("
floatXXX
",(double)fV);
Nightmare,then1.1Save it and becomes a value 1.00999999046326,No matter how you test it, it's wrong,What the hell is this
Baidu/s?ie=UTF-8&amp;tn=62095104_35_oem_dg&amp;wd=1.00999999046326There is also this inexplicable number
Unexpectedly,Then downloaded/paiden/NettCheck out the source code:

// Values
public static Result<TomlBool> Update(this TomlTable table, string key, bool value)
=> Update(table, key, (value));

public static Result<TomlString> Update(this TomlTable table, string key, string value)
=> Update(table, key, (value));

public static Result<TomlInt> Update(this TomlTable table, string key, long value)
=> Update(table, key, (value));

public static Result<TomlFloat> Update(this TomlTable table, string key, double value)
=> Update(table, key, (value));

public static Result<TomlOffsetDateTime> Update(this TomlTable table, string key, DateTimeOffset value)
=> Update(table, key, (value));

public static Result<TomlDuration> Update(this TomlTable table, string key, TimeSpan value)
=> Update(table, key, (value));

I figured out some tricks, there was no float type, so I changed it to double, everything was calm and returned to normal.

OMG, this one. . . .

To draw a conclusion, please use double to read non-integrated numbers in C# using toml files. Do not use float. It doesn’t matter if decimal is not a problem. Anyway, it doesn’t matter if it is compiled, remember not to use float.

I will record it here to avoid confusion. It is also considered a less useful knowledge point in the program, so it is a record.

This is the end of this article about the tricks of using toml in c# as configuration files. For more related contents of c# toml configuration files, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!