Help: Can't format string using String.Format
-
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 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;
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
-
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;
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 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;
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.
-
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 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;
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
-
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;
You could try Substring.... :-O
-
You could try Substring.... :-O
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.
-
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.
He seems to want to round a string.
-
He seems to want to round a string.
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.
-
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.
Luc Pattyn wrote:
he should parse and either round or format
I disagree. :-D