How to format string with number value?

Tim Kohlmeyer 0 Reputation points
2023-03-03T09:16:59.1033333+00:00

Hey Community,

I have a string which I want to transform from:

5000000,00

to

5.000.000,00

I hope that you can help me. The programming language is C#. I need a replacement which is dynamically because the string can have values like 5,00, 1.000,00, 1.000.000.000,00 etc.....

Best regards

Timgo1001

Developer technologies Windows Forms
Developer technologies C#
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Olaf Helper 47,436 Reputation points
    2023-03-03T09:21:28.0666667+00:00

    For this you can use a format string, see

    Standard numeric format strings

    Custom numeric format strings

    e.g. N2 for numeric with 2 decimals.


  2. Viorel 122.5K Reputation points
    2023-03-03T10:35:56.44+00:00

    The string can be converted to number before formatting, for example:

    string s = "5000000,00";
    var c = CultureInfo.GetCultureInfo( "de" );
    string result = decimal.Parse( s, c ).ToString( "#,##0.00", c );
    // Result: 5.000.000,00
    

    Use the appropriate culture.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.