Results 1 to 12 of 12

Thread: [RESOLVED] how to format double in custom format

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2016
    Posts
    566

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

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,905

    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++ ?

  3. #3
    PowerPoster
    Join Date
    Nov 2017
    Posts
    2,760

    Re: how to format double in custom format

    Quote Originally Posted by DaveDavis View Post
    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.
    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.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2016
    Posts
    566

    Re: how to format double in custom format

    Quote Originally Posted by si_the_geek View Post
    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.

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2016
    Posts
    566

    Re: how to format double in custom format

    Quote Originally Posted by OptionBase1 View Post
    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.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109,805

    Re: how to format double in custom format

    Quote Originally Posted by DaveDavis View Post
    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.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2016
    Posts
    566

    Re: how to format double in custom format

    Quote Originally Posted by jmcilhinney View Post
    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# .

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109,805

    Re: how to format double in custom format

    Quote Originally Posted by DaveDavis View Post
    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.

  9. #9
    Fanatic Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    885

    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)

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2016
    Posts
    566

    Re: how to format double in custom format

    oh, it is very nice. My project is using c++ 14, no wonder I didn't see it. Thank you Sir.

  11. #11
    Fanatic Member 2kaud's Avatar
    Join Date
    May 2014
    Location
    England
    Posts
    885

    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)

  12. #12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width