Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Help: Can't format string using String.Format

Help: Can't format string using String.Format

Scheduled Pinned Locked Moved C#
helptutorial
11 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    roman_s
    wrote on last edited by
    #1

    I cant seem to format my strAverageDensity which originally is in this example strAverageDensity = 30.000000123 using: string.Format("{0:0.00}", strAverageDensity); need it to equal 30.00 in this case but 2 decimal places instead its still strAverageDensity = 30.000000123 so this is the code:

                EditSubCategory E = new EditSubCategory();
    
                    SubCategory C = GlobalContextManager.Instance.Cache.GetSubCategory((int)R.Cells\[0\].Value);
                    Category CC = GlobalContextManager.Instance.Cache.GetCategory(C.CategoryId);
    
                  string strAverageDensity = Convert.ToString(GlobalContextManager.Instance.Conversions.ConvertMetricWeightToDefault(C.AverageDensity));
    
                    E.ParentCategory = CC;
                    E.Category = C.Name;
                    E.IsActivated = C.IsEnabled;
                    E.OriginalCategory = C.Name;
    
                    strAverageDensity = string.Format("{0:0.00}", strAverageDensity);
                    E.AverageDensity = strAverageDensity;
    
    E OriginalGriffO L I M 6 Replies Last reply
    0
    • R roman_s

      I cant seem to format my strAverageDensity which originally is in this example strAverageDensity = 30.000000123 using: string.Format("{0:0.00}", strAverageDensity); need it to equal 30.00 in this case but 2 decimal places instead its still strAverageDensity = 30.000000123 so this is the code:

                  EditSubCategory E = new EditSubCategory();
      
                      SubCategory C = GlobalContextManager.Instance.Cache.GetSubCategory((int)R.Cells\[0\].Value);
                      Category CC = GlobalContextManager.Instance.Cache.GetCategory(C.CategoryId);
      
                    string strAverageDensity = Convert.ToString(GlobalContextManager.Instance.Conversions.ConvertMetricWeightToDefault(C.AverageDensity));
      
                      E.ParentCategory = CC;
                      E.Category = C.Name;
                      E.IsActivated = C.IsEnabled;
                      E.OriginalCategory = C.Name;
      
                      strAverageDensity = string.Format("{0:0.00}", strAverageDensity);
                      E.AverageDensity = strAverageDensity;
      
      E Offline
      E Offline
      Ennis Ray Lynch Jr
      wrote on last edited by
      #2

      strAverageDensity should be of type float or double and not string to take advantage of numeric format strings.

      Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

      1 Reply Last reply
      0
      • R roman_s

        I cant seem to format my strAverageDensity which originally is in this example strAverageDensity = 30.000000123 using: string.Format("{0:0.00}", strAverageDensity); need it to equal 30.00 in this case but 2 decimal places instead its still strAverageDensity = 30.000000123 so this is the code:

                    EditSubCategory E = new EditSubCategory();
        
                        SubCategory C = GlobalContextManager.Instance.Cache.GetSubCategory((int)R.Cells\[0\].Value);
                        Category CC = GlobalContextManager.Instance.Cache.GetCategory(C.CategoryId);
        
                      string strAverageDensity = Convert.ToString(GlobalContextManager.Instance.Conversions.ConvertMetricWeightToDefault(C.AverageDensity));
        
                        E.ParentCategory = CC;
                        E.Category = C.Name;
                        E.IsActivated = C.IsEnabled;
                        E.OriginalCategory = C.Name;
        
                        strAverageDensity = string.Format("{0:0.00}", strAverageDensity);
                        E.AverageDensity = strAverageDensity;
        
        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        Try:

        strAverageDensity = string.Format("{0:0.00}", C.AverageDensity);

        which I assume is a float / double. Strings do not use numeric formats!

        Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        1 Reply Last reply
        0
        • R roman_s

          I cant seem to format my strAverageDensity which originally is in this example strAverageDensity = 30.000000123 using: string.Format("{0:0.00}", strAverageDensity); need it to equal 30.00 in this case but 2 decimal places instead its still strAverageDensity = 30.000000123 so this is the code:

                      EditSubCategory E = new EditSubCategory();
          
                          SubCategory C = GlobalContextManager.Instance.Cache.GetSubCategory((int)R.Cells\[0\].Value);
                          Category CC = GlobalContextManager.Instance.Cache.GetCategory(C.CategoryId);
          
                        string strAverageDensity = Convert.ToString(GlobalContextManager.Instance.Conversions.ConvertMetricWeightToDefault(C.AverageDensity));
          
                          E.ParentCategory = CC;
                          E.Category = C.Name;
                          E.IsActivated = C.IsEnabled;
                          E.OriginalCategory = C.Name;
          
                          strAverageDensity = string.Format("{0:0.00}", strAverageDensity);
                          E.AverageDensity = strAverageDensity;
          
          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          you failed to show an essential line of source code. Now compare yours to this:

          		float f=30.01234f;
          		log(string.Format("{0:0.00}", f));
          		string s="30.01234";
          		log(string.Format("{0:0.00}", s));
          

          :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

          Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

          1 Reply Last reply
          0
          • R roman_s

            I cant seem to format my strAverageDensity which originally is in this example strAverageDensity = 30.000000123 using: string.Format("{0:0.00}", strAverageDensity); need it to equal 30.00 in this case but 2 decimal places instead its still strAverageDensity = 30.000000123 so this is the code:

                        EditSubCategory E = new EditSubCategory();
            
                            SubCategory C = GlobalContextManager.Instance.Cache.GetSubCategory((int)R.Cells\[0\].Value);
                            Category CC = GlobalContextManager.Instance.Cache.GetCategory(C.CategoryId);
            
                          string strAverageDensity = Convert.ToString(GlobalContextManager.Instance.Conversions.ConvertMetricWeightToDefault(C.AverageDensity));
            
                            E.ParentCategory = CC;
                            E.Category = C.Name;
                            E.IsActivated = C.IsEnabled;
                            E.OriginalCategory = C.Name;
            
                            strAverageDensity = string.Format("{0:0.00}", strAverageDensity);
                            E.AverageDensity = strAverageDensity;
            
            I Offline
            I Offline
            ignrod
            wrote on last edited by
            #5

            As has already been pointed out, this format string works with numeric types. Why don't you try?

            double dAverageDensity = Double.Parse(strAverageDensity);
            strAverageDensity = String.Format("{0:0.00}", dAverageDensity);

            1 Reply Last reply
            0
            • R roman_s

              I cant seem to format my strAverageDensity which originally is in this example strAverageDensity = 30.000000123 using: string.Format("{0:0.00}", strAverageDensity); need it to equal 30.00 in this case but 2 decimal places instead its still strAverageDensity = 30.000000123 so this is the code:

                          EditSubCategory E = new EditSubCategory();
              
                              SubCategory C = GlobalContextManager.Instance.Cache.GetSubCategory((int)R.Cells\[0\].Value);
                              Category CC = GlobalContextManager.Instance.Cache.GetCategory(C.CategoryId);
              
                            string strAverageDensity = Convert.ToString(GlobalContextManager.Instance.Conversions.ConvertMetricWeightToDefault(C.AverageDensity));
              
                              E.ParentCategory = CC;
                              E.Category = C.Name;
                              E.IsActivated = C.IsEnabled;
                              E.OriginalCategory = C.Name;
              
                              strAverageDensity = string.Format("{0:0.00}", strAverageDensity);
                              E.AverageDensity = strAverageDensity;
              
              M Offline
              M Offline
              Martin shooster
              wrote on last edited by
              #6

              hi dear friend. i am a regular programmer,but i think :doh: this line of your program can be change string strAverageDensity = Convert.ToString(GlobalContextManager.Instance.Conversions.ConvertMetricWeightToDefault(C.AverageDensity)); instead of Convert.ToString(...) use of System.Convert.ToString(...). i hope success for you. M.Shooster

              1 Reply Last reply
              0
              • R roman_s

                I cant seem to format my strAverageDensity which originally is in this example strAverageDensity = 30.000000123 using: string.Format("{0:0.00}", strAverageDensity); need it to equal 30.00 in this case but 2 decimal places instead its still strAverageDensity = 30.000000123 so this is the code:

                            EditSubCategory E = new EditSubCategory();
                
                                SubCategory C = GlobalContextManager.Instance.Cache.GetSubCategory((int)R.Cells\[0\].Value);
                                Category CC = GlobalContextManager.Instance.Cache.GetCategory(C.CategoryId);
                
                              string strAverageDensity = Convert.ToString(GlobalContextManager.Instance.Conversions.ConvertMetricWeightToDefault(C.AverageDensity));
                
                                E.ParentCategory = CC;
                                E.Category = C.Name;
                                E.IsActivated = C.IsEnabled;
                                E.OriginalCategory = C.Name;
                
                                strAverageDensity = string.Format("{0:0.00}", strAverageDensity);
                                E.AverageDensity = strAverageDensity;
                
                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #7

                You could try Substring.... :-O

                L 1 Reply Last reply
                0
                • P PIEBALDconsult

                  You could try Substring.... :-O

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  substring is chopping strings, not rounding numbers. :)

                  Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                  Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                  P 1 Reply Last reply
                  0
                  • L Luc Pattyn

                    substring is chopping strings, not rounding numbers. :)

                    Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                    Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                    P Offline
                    P Offline
                    PIEBALDconsult
                    wrote on last edited by
                    #9

                    He seems to want to round a string.

                    L 1 Reply Last reply
                    0
                    • P PIEBALDconsult

                      He seems to want to round a string.

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #10

                      Yes it sure looks that way, although he never said it explicitly. So he should parse and either round or format, as ignrod suggested. :)

                      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                      Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                      P 1 Reply Last reply
                      0
                      • L Luc Pattyn

                        Yes it sure looks that way, although he never said it explicitly. So he should parse and either round or format, as ignrod suggested. :)

                        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                        Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                        P Offline
                        P Offline
                        PIEBALDconsult
                        wrote on last edited by
                        #11

                        Luc Pattyn wrote:

                        he should parse and either round or format

                        I disagree. :-D

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups