将string numStr = "1234.56789";分别转换为int ,float , double,并输出结果

解决方案 »

  1.   

    强转啊int.parse(numStr.tostring());依次类推 
      

  2.   

    本帖最后由 caozhy 于 2011-07-06 10:12:17 编辑
      

  3.   

    int.parse(numStr)
    double.parse(numStr)
    single.parse(numStr)
      

  4.   


    不行,出现错误

    未处理 System.FormatException
      Message=输入字符串的格式不正确。
      Source=mscorlib
      StackTrace:
           在 System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
           在 System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
           在 System.Convert.ToInt32(String value)
           在 PB_Topic1_8.Program.Main(String[] args) 位置 E:\1111\111\Topic1\PB_Topic1_8\PB_Topic1_8\Program.cs:行号 16
           在 System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
           在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
           在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           在 System.Threading.ThreadHelper.ThreadStart()
      InnerException: 
      

  5.   


    这样也不行,未处理 System.FormatException
      Message=输入字符串的格式不正确。
      Source=mscorlib
      StackTrace:
           在 System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
           在 System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
           在 System.Int32.Parse(String s)
           在 PB_Topic1_8.Program.Main(String[] args) 位置 E:\学习\作业\Topic1\PB_Topic1_8\PB_Topic1_8\Program.cs:行号 18
           在 System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
           在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
           在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           在 System.Threading.ThreadHelper.ThreadStart()
      InnerException: 
      

  6.   

    string numStr = "1234.56789";double d = Convert.ToDouble(numStr);int i = d;
    float f = (float)d;哎  基本功挺差的昂
      

  7.   

    string numStr = "1234.56789";double d = Convert.ToDouble(numStr);int i = d;
    float f = (float)d;楼上这个方法肯定能过,就是准确就达不到那么精确了
      

  8.   


    谢谢,int i =(int) d;
    就OK了