1

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

2 Answers 2

2

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);
Sign up to request clarification or add additional context in comments.

5 Comments

This will round the number to 6 decimal places. It doesn't sound like that's what he wants, since he's got a 7-decimal-place number in the sample he provided.
thank you @dai .if i do it, it shows the result always at the same as my system?
@StriplingWarrior It is an exercise left-up to the reader to modify it to use the desired number of decimal places :)
@Dai: There is no number that would yield the desired results listed by the OP. You'll either round too low or add trailing zeroes. The point of the question is that the format changes depending on the user's system settings. I see that you added InvariantCulture: that is the correct approach. "F6" as a format string isn't. :-)
@StriplingWarrior Well, both - but using F6 also means you won't get the Exponential Notation.
1

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);

4 Comments

thank you @StriplingWarrior .if i do it, it shows the result always at the same as my system?
@user3446681: Yes, the InvariantCulture is meant to stay the same across different systems.
thanks to much @StriplingWarrior. Also May you tell me what is difference between your method and Dai (comment above)
@user3446681: Yes. Dai's answer focuses on using a format specifier to indicate a fixed number of decimal spaces. He does this to prevent exponential notation from ever happening. My approach could potentially switch to that E^-12 mode, for example, if the numbers are small enough. I focused on the issue of the format changing from one machine to the next, although you may want to try a custom format if you need to ensure a specific format all the time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.