https://github.com/jaytwolab/to_string-floating-type
convert floating type to string (Modern C++)
https://github.com/jaytwolab/to_string-floating-type
Last synced: about 2 months ago
JSON representation
convert floating type to string (Modern C++)
- Host: GitHub
- URL: https://github.com/jaytwolab/to_string-floating-type
- Owner: JayTwoLab
- License: mit
- Created: 2024-07-30T05:52:06.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2024-10-07T04:25:41.000Z (8 months ago)
- Last Synced: 2025-03-28T03:07:34.287Z (about 2 months ago)
- Language: C++
- Homepage: https://jaytwolab.github.io/to_string-floating-type/
- Size: 14.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# to_string-floating-type
- C++ function to convert floating type to string ```to_string```
```cpp
void test1(void)
{
float f = 10.12f;
double d = 10.12;std::cout << std::fixed
<< f // 10.120000
<< " "
<< d // 10.120000
<< std::endl;
std::cout << std::fixed
<< j2::to_string(f) // 10.119999886
<< " "
<< j2::to_string(d) // 10.11999999999999922
<< std::endl;
std::cout << std::fixed
<< j2::to_string(f, 2) // 10.12
<< " "
<< j2::to_string(d, 2) // 10.12
<< std::endl;
std::cout << std::fixed
<< j2::to_string(f,10) // 10.1199998856
<< " "
<< j2::to_string(d,20) // 10.11999999999999921840
<< std::endl;
}
```- C++ function to compare floating type to string ```is_equal```
```cpp
void test_double(double d1, double d2, long long precision)
{
bool ret = j2::is_equal(d1, d2, precision);if (ret) { std::cout << "[equal] "; } else { std::cout << "[not equal] "; }
std::cout << std::fixed
<< j2::to_string(d1, precision) << " " << j2::to_string(d2, precision)
<< std::endl;
}void test2()
{
test_double(10.12, 10.12, 2); // [equal] 10.12 10.12
test_double(10.12, 10.1, 1); // [equal] 10.1 10.1
test_double(10.12, 10.12, 3); // [equal] 10.120 10.120
test_double(10.12, 10.121, 3); // [not equal] 10.120 10.121
test_double(10.12, 10.121, 20); // [not equal] 10.11999999999999921840 10.12100000000000044054
test_double(10.12, 10.1201, 20); // [not equal] 10.11999999999999921840 10.12010000000000076170
test_double(10.12, 10.12001, 20); // [not equal] 10.11999999999999921840 10.12001000000000061618
test_double(10.12, 10.120001, 20); // [not equal] 10.11999999999999921840 10.12000100000000024636
test_double(10.12, 10.1200001, 20); // [not equal] 10.11999999999999921840 10.12000010000000038701
test_double(10.12, 10.12, 10); // [equal] 10.1200000000 10.1200000000
}
```## License
- to_string-floating-type is under MIT License. https://github.com/j2doll/to_string-floating-type
- It is a part of JayTwo(j2) Library.