-
Apr 20th, 2023, 09:41 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] how to format double in custom format
I have c# code to format double:
Code:
string str= "12.123456";
string strFormat = "G";
strOutput = Convert.ToDouble(str.Replace(",", "")).ToString(strFormat); //12.123456
str = "-12.1";
strFormat = "'正'0.00;'负'0.00;零";
strOutput = Convert.ToDouble(str.Replace(",", "")).ToString(strFormat); //负12.10
str = "12345.6789";
strFormat = "#,##0.00";
strOutput = Convert.ToDouble(str.Replace(",", "")).ToString(strFormat); //12,345.68
how to write a general function with custom format string as C#'s ToString(format)?
ChatGPT told me I can use #include <System.SysUtils.hpp> (from Delphi/C++Builder library), but I don't know how to find the hpp file.
-
Apr 20th, 2023, 01:58 PM
#2
Re: how to format double in custom format
The way you've phrased things isn't totally clear, are you aiming for something (almost) equivalent to {double}.ToString(strFormat) in C++ ?
-
Apr 20th, 2023, 04:22 PM
#3
Re: how to format double in custom format
-
Apr 20th, 2023, 04:35 PM
#4
Thread Starter
Fanatic Member
Re: how to format double in custom format
 Originally Posted by si_the_geek
The way you've phrased things isn't totally clear, are you aiming for something (almost) equivalent to {double}.ToString(strFormat) in C++ ?
Is it impossible for C#'s ToString(strFormat) Or VB.NET's Strings.Format in c++?
I can do simple formatting.
C#: strFormatDouble = ToDouble(str.Replace(",", "")).ToString("##,##0." + new string('0', 2));
string CHAR_DECIMAL = ".";
string CHAR_THOUSAND = ",";
strFormatDouble = ToDouble(str.Replace(CHAR_THOUSAND, "")).ToString("##" + CHAR_DECIMAL + "##0." + new string('0', decimalLength));
c++:
_bstr_t FormatDecimalString(std::string str, int decimalLength)
{
double d = std::stod(str);
std:  stringstream oss;
oss << std::fixed << std::setprecision(decimalLength) << d;
std::string strFormatDouble = oss.str();
size_t pos = strFormatDouble .find(CHAR_DECIMAL);
if (pos != std::string::npos) {
int digits = pos - (strFormatDouble [0] == '-' ? 1 : 0);
if (digits >= 4) {
std::string strThousand(1, CHAR_THOUSAND);
for (int i = 3; i < digits; i += 3) {
strFormatDouble .insert(pos - i, strThousand);
}
}
}
return strFormatDouble .c_str();
}
Last edited by DaveDavis; Apr 20th, 2023 at 05:02 PM.
-
Apr 20th, 2023, 04:45 PM
#5
Thread Starter
Fanatic Member
Re: how to format double in custom format
 Originally Posted by OptionBase1
Did you ask ChatGPT where you could find the file?
If you aren't using Borland/Abracadabra C++ Builder, then that's a non-starter suggestion. I have BCB C++ 3.0 handy, and I can tell you that the SysUtils.hpp file has a large import-chain of several other Borland specific .hpp and .h files.
I like FormatFloat in System::Sysutils. If the real world has similar thing in c++ cpp, please tell me. I don't want (know how) to create such complex wheel.
-
Apr 20th, 2023, 07:24 PM
#6
Re: how to format double in custom format
 Originally Posted by DaveDavis
Is it impossible for C#'s ToString(strFormat)
It's not C#'s anything. That's a standard method, available to any and all .NET languages. If you're writing managed C++ (is it C++/CLI they call it?) then you can use it too. If you're not then you can't but I'm certain that C++ supports composite formatting, a la String.Format in .NET. It's so long since I used C++ I can;t recall how though. That's what you should be looking for, in that case.
-
Apr 20th, 2023, 08:22 PM
#7
Thread Starter
Fanatic Member
Re: how to format double in custom format
 Originally Posted by jmcilhinney
It's not C#'s anything. That's a standard method, available to any and all .NET languages. If you're writing managed C++ (is it C++/CLI they call it?) then you can use it too. If you're not then you can't but I'm certain that C++ supports composite formatting, a la String.Format in .NET. It's so long since I used C++ I can;t recall how though. That's what you should be looking for, in that case.
Thank you Sir. Nice to get your input.
In VB.NET, I use Strings.Format. In C#, I am using double.ToString(myformat) to do formatting. But I have no idea how to do same thing in vc++ ATL.
I can't write meaningful reply because I am very new in c++. I am using vc++ ATL and I am in draft stage of conversion from one of my WinForms component in vb.NET and c# .
-
Apr 20th, 2023, 08:50 PM
#8
Re: how to format double in custom format
 Originally Posted by DaveDavis
In VB.NET, I use Strings.Format. In C#, I am using double.ToString(myformat) to do formatting.
Well that's silly for a start. That Strings.Format is basically a holdover from VB6 and should only be used in upgraded VB6 code that already uses it. The "proper" .NET equivalent is String.Format, which can be used in VB, C# and any other .NET language. They are both for composite formatting though, so if you're just formatting a single value then you should call its ToString method that, again, can be used in VB, C# and any other .NET language.
I think that what you probably want in C++ is the sprintf function.
-
Apr 21st, 2023, 03:51 AM
#9
Re: how to format double in custom format
All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
Apr 21st, 2023, 05:37 PM
#10
Thread Starter
Fanatic Member
Re: how to format double in custom format
 Originally Posted by 2kaud
oh, it is very nice. My project is using c++ 14, no wonder I didn't see it. Thank you Sir.
-
Apr 22nd, 2023, 04:30 AM
#11
Re: [RESOLVED] how to format double in custom format
My project is using c++ 14
I suggest you update to at least C++20 (C++23 will be released shortly - it's currently in final draft). There's been very many major and important additions to C++ since the C++14 standard was issued (there's now a new standard every 3 years).
All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
Apr 22nd, 2023, 04:55 AM
#12
Re: [RESOLVED] how to format double in custom format
For most flexibility and speed (esp. multi-threaded) you can use this: https://github.com/fmtlib/fmt
cheers,
</wqw>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|