小数点后的值 [英] Values after decimal

查看:83
本文介绍了小数点后的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将此字符串格式"String.Format("{0:0.00}", mytextbox.Text);"应用于我的mytextbox

但我也希望它包括以下课程

I want to apply this string format "String.Format("{0:0.00}", mytextbox.Text);" to my mytextbox

But I also want it to include following class

public class DoubleFormatter : IFormatProvider, ICustomFormatter
{
  // always use dot separator for doubles
  private CultureInfo enUsCulture = CultureInfo.GetCultureInfo("en-US");

  public string Format(string format, object arg, IFormatProvider formatProvider)
  {
    // format doubles to 2 decimal places
    return string.Format(enUsCulture, "{0:0.00}", arg);
  }

  public object GetFormat(Type formatType)
  {
    return (formatType == typeof(ICustomFormatter)) ? this : null;
  }
}



但是问题是我不知道在哪里写字符串格式或如何写.
简而言之困惑.
帮帮我.



But the issue is I don''t know where to write string format or how.
Inshort confused.
Help me.

推荐答案

您已阅读文档吗? http://msdn.microsoft.com/en-us/library/1ksz8yb7.aspx [ ^ ]
Have you read the documentation? http://msdn.microsoft.com/en-us/library/1ksz8yb7.aspx[^]


由于您已经在DoubleFormatter 类中使用了String.Format,因此无需再次调用它.而是直接使用您的DoubleFormatter 类.

调用Format,由于该函数未使用参数,因此将null 作为format 传递,将mytextbox.Text作为arg传递,并将null 作为formatProvider 传递(因为也未使用该参数).您可能需要将mytextbox.Text 转换为double,如果这样,则可以使用Double.TryParse 进行此操作.
Since you are already using the String.Format in your DoubleFormatter class, there is no need to call it again. Instead directly use your DoubleFormatter class.

Call Format, and pass null as format since that parameter is not used by the function, pass mytextbox.Text as arg, and pass null as formatProvider (since that parameter is not used either). You may need to convert mytextbox.Text to a double, and if that''s so then you can use Double.TryParse to do that.


这篇关于小数点后的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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