我尝试将数据类型double转换为String..Help时出错 [英] I have had an error trying to convert data type double to String..Help

查看:85
本文介绍了我尝试将数据类型double转换为String..Help时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了以下内容......

I have tried the following...

public static void String Detail()
{  
  double minute = duration /60; 
  return minute.ToString();
}

推荐答案

public static void String Detail()



我甚至对这个编译感到惊讶。你的方法的返回类型是什么? void或String?



另一个问题是:您的问题是从持续时间中查找分钟数,还是显示双数字的字符串表示?



如果你正在努力计算几秒钟的分钟数,那肯定是:


I'm even surprised this compiles. What is the return type of your method? void or String?

Another question is: is your issue finding the number of minutes from a duration, or displaying a string representation of a double number?

If you're struggling with computing the number of minutes from a number of seconds, then it's definitely:

public static double minutesFromSeconds(int seconds) {
   return seconds / 60.0;
   // Here we use the 60.0 notation so that we compute a real division,
   // not an integer one
}





如果您的问题是获得双数字的字符串表示,那么它就像以下一样简单:



If your issue is getting a string representation of a double number, then it is as trivial as:

String result = minutes.ToString();





重要的是:永远不要使用任何数字作为字符串。仅在需要将其呈现给用户时才使用字符串表示。使用字符串来处理代码中的数字是最糟糕的做法之一。



The important thing is: never work with any number as a string. Only use a string representation when you need to present it to the user. Using strings to handle numbers in code is one of the worst practices ever.


Aha,静态方法使用未声明的持续时间。怎么会这样?它还是 static 吗?馊主意。毕竟把持续时间作为参数传递给方法......

但即使你修复它,这种方法也没什么意义,但这是一个不同的故事。

< br $> b $ b



对不起,我没注意到void和string是两种不同的返回类型,所以它不能编译。我注意到它只能归功于解决方案2.



感谢您正式接受答案。如果您有其他问题,请再次致电。



-SA
Aha, static method using undeclared duration. How so? Is it also static? Bad idea. Pass duration as a parameter to the method, after all…
But even if you fix it, such method makes too little sense, but this is a different story.



And sorry, I did not notice that "void" and "string" are two different return types, so it cannot compile. I noticed it only thanks to Solution 2.

Thank you for accepting the answers formally. Please call again if you have other questions.

—SA


这篇关于我尝试将数据类型double转换为String..Help时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆