【问题标题】:Newbie C# Question about float/int/text type formatting新手 C# 关于 float/int/text 类型格式化的问题
【发布时间】:2011-06-03 19:43:47
【问题描述】:

我是一个 C# 新手,在 Python 中具有轻度(第一年的 CS)背景。我用 Python 编写了一个控制台程序,用于进行马拉松配速计算,我正在尝试使用 Visual Studio 2010 在 C# 中找出它的语法。这是我目前所掌握的大部分内容:

string total_seconds = ((float.Parse(textBox_Hours.Text) * 60 * 60) + (float.Parse(textBox_Minutes.Text) * 60) + float.Parse(textBox_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;

假设您的跑步配速为每英里 8 分 30 秒。 secs_per_unit 为 510,mins_per_unit 为 8.5。 pace_mins 将简单地为 8,pace_secs 将是 30。例如,在 Python 中,我只需将变量从浮点数转换为字符串以获得 8 而不是 8.5;希望其余的代码能让您了解我一直在做什么。

任何意见将不胜感激。

【问题讨论】:

  • 最好在 textBox_final_Mins 和 textBox_Final_Secs 中指定你现在得到的内容。而且,你想要什么

标签: c# types floating-point integer


【解决方案1】:

试试这个。总体而言,将事物更多地存储为整数,而不是存储为浮点数并多次转换为整数。并且直到最后一刻才转换为字符串。

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

【讨论】:

    【解决方案2】:

    小时和分钟应该只取整数,因为你已经用了秒(没有意义有 1.5 小时 30 分钟而不是 2 小时 0 分钟)。

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

    写得很快,还没有测试过。但是这样的东西应该可以工作,至少有非常小的调整/错别字修复。

    【讨论】:

      【解决方案3】:

      如果要截断分数,可以将浮点数转换为字符串

      .ToString("F0")
      

      如果你改写你的问题会更好。

      【讨论】:

        【解决方案4】:

        您可以使用cast operators and conversion functions

        这将是一个演员表:

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

        这将是一个转换:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-02-01
          • 1970-01-01
          • 2021-12-15
          • 2023-02-05
          相关资源
          最近更新 更多