开发者

Force Excel to understand numbers the way I formatted them

开发者 https://www.devze.com 2023-01-24 00:49 出处:网络
I did some benchmarking experiments on a software, and now I must plot the results of the round-trip time.

I did some benchmarking experiments on a software, and now I must plot the results of the round-trip time.

During the experiment, we generated lots of messages in Syslog format (RFC 5424) indicating how much time it took for a message to be processed by the program under different and controlled test conditions. Now we have several logs, 1 from each of 6 machines, for an almost entire day of experimenting.

I decided to use Excel to plot a graph of time of experiment and RTT, so I created a little C# utility to parse Syslog messages and convert them into CSV selecting the fields I want.

The following is an example of some Syslog messages for the experiment

<167>1 2010-10-07T13:29:53+02:00 rosy717 RTTMonitor 4669 - [Call开发者_如何学PythonerData@8289 ClassName="RTTMonitor.Program" MethodName="source_MessageReceived" ModuleName="RTTMonitor" LogName="Logfile"][timeQuality tzKnown="1"][origin ip="10.0.0.58" enterpriseId="8289" software="Logbus-ng-sharp" swVersion="1.8.3"][meta sequenceId="50"] RTT: 12.353
<167>1 2010-10-07T13:29:56+02:00 rosy717 RTTMonitor 4669 - [CallerData@8289 ClassName="RTTMonitor.Program" MethodName="source_MessageReceived" ModuleName="RTTMonitor" LogName="Logfile"][timeQuality tzKnown="1"][origin ip="10.0.0.58" enterpriseId="8289" software="Logbus-ng-sharp" swVersion="1.8.3"][meta sequenceId="51"] RTT: 12.775
<167>1 2010-10-07T13:29:59+02:00 rosy717 RTTMonitor 4669 - [CallerData@8289 ClassName="RTTMonitor.Program" MethodName="source_MessageReceived" ModuleName="RTTMonitor" LogName="Logfile"][timeQuality tzKnown="1"][origin ip="10.0.0.58" enterpriseId="8289" software="Logbus-ng-sharp" swVersion="1.8.3"][meta sequenceId="52"] RTT: 12.979
<167>1 2010-10-07T13:30:02+02:00 rosy717 RTTMonitor 4669 - [CallerData@8289 ClassName="RTTMonitor.Program" MethodName="source_MessageReceived" ModuleName="RTTMonitor" LogName="Logfile"][timeQuality tzKnown="1"][origin ip="10.0.0.58" enterpriseId="8289" software="Logbus-ng-sharp" swVersion="1.8.3"][meta sequenceId="53"] RTT: 20.899
<167>1 2010-10-07T13:30:05+02:00 rosy717 RTTMonitor 4669 - [CallerData@8289 ClassName="RTTMonitor.Program" MethodName="source_MessageReceived" ModuleName="RTTMonitor" LogName="Logfile"][timeQuality tzKnown="1"][origin ip="10.0.0.58" enterpriseId="8289" software="Logbus-ng-sharp" swVersion="1.8.3"][meta sequenceId="54"] RTT: 12.971
<167>1 2010-10-07T13:30:08+02:00 rosy717 RTTMonitor 4669 - [CallerData@8289 ClassName="RTTMonitor.Program" MethodName="source_MessageReceived" ModuleName="RTTMonitor" LogName="Logfile"][timeQuality tzKnown="1"][origin ip="10.0.0.58" enterpriseId="8289" software="Logbus-ng-sharp" swVersion="1.8.3"][meta sequenceId="55"] RTT: 12.419
<167>1 2010-10-07T13:30:11+02:00 rosy717 RTTMonitor 4669 - [CallerData@8289 ClassName="RTTMonitor.Program" MethodName="source_MessageReceived" ModuleName="RTTMonitor" LogName="Logfile"][timeQuality tzKnown="1"][origin ip="10.0.0.58" enterpriseId="8289" software="Logbus-ng-sharp" swVersion="1.8.3"][meta sequenceId="56"] RTT: 12.34
<167>1 2010-10-07T13:30:14+02:00 rosy717 RTTMonitor 4669 - [CallerData@8289 ClassName="RTTMonitor.Program" MethodName="source_MessageReceived" ModuleName="RTTMonitor" LogName="Logfile"][timeQuality tzKnown="1"][origin ip="10.0.0.58" enterpriseId="8289" software="Logbus-ng-sharp" swVersion="1.8.3"][meta sequenceId="57"] RTT: 13.622
<167>1 2010-10-07T13:30:17+02:00 rosy717 RTTMonitor 4669 - [CallerData@8289 ClassName="RTTMonitor.Program" MethodName="source_MessageReceived" ModuleName="RTTMonitor" LogName="Logfile"][timeQuality tzKnown="1"][origin ip="10.0.0.58" enterpriseId="8289" software="Logbus-ng-sharp" swVersion="1.8.3"][meta sequenceId="58"] RTT: 12.873
<167>1 2010-10-07T13:30:20+02:00 rosy717 RTTMonitor 4669 - [CallerData@8289 ClassName="RTTMonitor.Program" MethodName="source_MessageReceived" ModuleName="RTTMonitor" LogName="Logfile"][timeQuality tzKnown="1"][origin ip="10.0.0.58" enterpriseId="8289" software="Logbus-ng-sharp" swVersion="1.8.3"][meta sequenceId="59"] RTT: 12.474

The messages are all formatted the same way. RTT times are in milliseconds. Unfortunately, when I generated these logs, I didn't care about specifying a fixed number of decimals, so some RTT results look like 12.34ms rather than 12.340ms, as I'm going to discuss

The conversion utility picks only some fields and generates a CSV file containing, in the final column, the timestamps. As you can see, the number format uses the decimal point. Unfortunately, in Italy we use decimal comma and point as group separator, so Excel ignores it and thinks 12.419 is 12419 and 12.34 is 1234. It would be OK if Excel thinks these numbers are microseconds, but 1234μs is 10 times less than 12419μs.

My question is

How to force Excel understand that these numbers are meant with decimal point? If I input a customized number format 0.000, those numbers that have 2 decimal digits are treated as strings and aligned to left

Alternatively

Since I can re-convert logs as many times as I want, but I wouldn't like to repeat the whole experiment, can you remind me how in C# do you convert a double to string forcing 3 decimals?

Thank you in advance

PS: I understand this question is between stackoverflow and superuser, but I also asked C# help in the second case


For your alternative, something like this should work:

myDouble.ToString("f3")

Or if you prefer:

string.Format("{0:f3}", myDouble)
0

精彩评论

暂无评论...
验证码 换一张
取 消