Reputation: 7
I have the below script which shows following format as output:
67534 : 0.645623
64653 : 0.854261
95688112 : 0.7566438
When I test my script on another system, the format of the second column(the float numbers) changed to something like: 12.3E^12. When I change the number format from the control panel, the format becomes okay. What is the solution to fix the format of numbers in my code instead of changing the number format of the system (only for second column (resultwithindex.result
))?
foreach (var resultwithindex in results.Select((r, index) => new { result = r, Index = index }).OrderByDescending(r => r.result).Take(n))
{
sb1.AppendFormat("{0}: {1}", c[resultwithindex.Index], resultwithindex.result);
sb1.AppendLine();
}
MessageBox.Show(sb1.ToString());
Thanks in advance
Upvotes: 1
Views: 69
Reputation: 154995
The key is to use the "F6" format string for your decimal values.
You can either do the ToString
call prior to sending it into AppendFormat
(like so:)
sb1.Appendformat "{0}: {1}", c[resultwithindex.Index], resultwithindex.result.ToString("F6", CultureInfo.InvariantCulture) );
...or take advantage of Composite Formatting strings and doing it like so:
sb1.Appendformat(CultureInfo.InvariantCulture, "{0}: {1:F6}", c[resultwithindex.Index], resultwithindex.result);
Upvotes: 2
Reputation: 156459
To avoid having the format of the strings depend on the culture settings for the machine that you're on, you'll want to specify a culture as a format provider. For example:
sb1.AppendFormat(CultureInfo.InvariantCulture, "{0}: {1}",
c[resultwithindex.Index], resultwithindex.result);
Upvotes: 1