开发者

Newbie C# Question about float/int/text type formatting

I'm a total C# newb with a light (first year CS) background in Python. I wrote a console program in Python for doing marathon pace running calculations and I'm trying to figure out the syntax for this in C# using Visual Studio 2010. Here's a chunk of what I've got so far:

string total_seconds = ((float.Parse(textBox_Hours.Text) * 60 * 60) + (float.Parse(textBox_Minutes.Text) * 60) + float.Parse(t开发者_StackOverflow社区extBox_Seconds.Text)).ToString();

float secs_per_unit = ((float)(total_seconds) / (float)(textBox_Distance.Text));
float mins_per_unit = (secs_per_unit / 60);

string pace_mins = (int)mins_per_unit.ToString();
string pace_secs = (float.Parse(mins_per_unit) - int.Parse(mins_per_unit) * 60).ToString();


textBox_Final_Mins.Text = pace_mins;
textBox_Final_Secs.Text = pace_mins;

Imagine you have a running pace of 8 minutes and 30 seconds per mile. secs_per_unit would be 510, mins_per_unit would be 8.5. pace_mins would simply be 8 and pace_secs would be 30. In Python I'd just convert variables from a float to a string to get 8 instead of 8.5, for example; hopefully the rest of the code gives you an idea of what I've been doing.

Any input would be appreciated.


For float to string if you want to cut off the fraction

.ToString("F0")

It would be better if you rephrase your question.


Hours and minutes should only take integers as you're already taking seconds (doesn't make sense to have have 1.5 hours and 30 minutes instead of just 2 hours 0 minutes).

var numHours = Convert.ToInt32(textBox_Hours.Text);
var numMinutes = Convert.ToInt32(textBox_Minutes.Text);
var numSeconds = Convert.ToDouble(textBox_Seconds.Text);

var totalDistance = Convert.ToDouble(textBox_Distance.Text);

var totalSeconds = ((numHours)*60) + numMinutes)*60 + numSeconds;

var secsPerUnit = totalSeconds/totalDistance;
var minsPerUnit = secsPerUnit/60;

var paceMinsStr = Math.Floor(minsPerUnit).ToString();

var paceSeconds = minsPerUnit - Math.Floor(minsPerUnit);
var paceSecondsStr = (paceSeconds/ 100 * 60).ToString();

Written quickly, haven't tested it.. but something like this should work, at least with very minor tweaks/typo fixes.


Try this. Overall, store things as integers more, rather than storing as floats and converting to integers multiple times. And don't convert to a string until the last moment.

// I'm assuming that the text boxes aren't intended to hold a fraction, 
// "8.5", for example. Therefore, use 'int' instead of 'float', and don't 
// convert to a string at the end.
int total_seconds = int.Parse(textBox_Hours.Text) * 60 * 60 + 
                    int.Parse(textBox_Minutes.Text) * 60 + 
                    int.Parse(textBox_Seconds.Text);

// you missed a Parse here.
// Use two separate variables for seconds per unit: 
// one for the total (510, in your example), one for just the seconds 
// portion of the Minute:Second display (30).
int total_secs_per_unit = (int)(total_seconds / float.Parse(textBox_Distance.Text));

int mins_per_unit = total_secs_per_unit / 60;
int secs_per_unit = total_secs_per_unit % 60;

string pace_mins = mins_per_unit.ToString();
string pace_secs = secs_per_unit.ToString();

textBox_Final_Mins.Text = pace_mins;
textBox_Final_Secs.Text = pace_secs;


You could use the cast operators and conversion functions.

This would be a cast:

double d = 1.2;
int i = (int)d;

This would be a conversion:

string s = "1";
int i = Convert.ToInt32(s);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜