简体   繁体   English

意外的数字格式输出

[英]unexpected number formatting output

I know there are plenty of question about this but I ddn't found anything reggarding this. 我知道对此有很多疑问,但我没有发现对此有任何反感。

I try to format number following this format : value.ToString("N", new CultureInfo("fr-CA")) . 我尝试按照以下格式格式化数字: value.ToString(“ N”,new CultureInfo(“ fr-CA”)) The type of value is float. 的类型为float。 The format apply but not the way I tought it would. 该格式适用,但我无法做到这一点。 I refered to this article about formatting 我参考了有关格式化的这篇文章

Here exemples : 这里的例子:

123456     display as 123 456.00
123456.789 display as 123 456.79 // rounded
1234.5     display 1 234.50      //added a 0 padding

Is there a way to format somthing like this ? 有没有办法格式化这样的东西?

123456     display as 123 456    
123456.789 display as 123 456.789 
1234.5     display as 1 234.5

Here is the code were I try to apply the formatting 这是我尝试应用格式的代码

private void dgvform_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    float val;
    if ( (e.ColumnIndex == valeur.Index || e.ColumnIndex == valeurEN.Index)
        && dgvform.Rows[e.RowIndex].Cells[type_d.Index].Value != null && dgvform.Rows[e.RowIndex].Cells[type_d.Index].Value.ToString() == "N"
        && dgvform.Rows[e.RowIndex].Cells[valeur.Index].Value != null
        && float.TryParse(dgvform.Rows[e.RowIndex].Cells[valeur.Index].Value.ToString(), out val))
    {
        if (e.ColumnIndex == valeurEN.Index)
            e.Value = val.ToString("N", new CultureInfo(_formatEN));
        else
            e.Value = string.Format("{0:# ### ###.####}", val);
    }
}

this is part of a datagridview. 这是datagridview的一部分。 the big if is to determinate if I need to apply formatting to this cell. 最大的if是确定是否需要将格式应用于此单元格。 valeur , valeurEN are both column were I need to apply formating. valeurvaleurEN都列都需要应用格式化。 e.Value = val.ToString("N", new CultureInfo(_formatEN)); is the old way I apply the formatting. 是我套用格式的旧方法。 _formatEN is basically "en-US". _formatEN基本上是“ en-US”。

Basically I want to add a thousand separator and be sure to use the good decimal separator any tought about what i'm doing wrong? 基本上我想添加一千个分隔符,并确保使用好十进制分隔符来说明我做错了什么吗?

/edit : value is a type of float / edit: 是浮点类型

I assume you would need to build a custom numeric format string . 我认为您需要构建一个自定义的数字格式字符串

value.ToString("#,0.##########", new CultureInfo("fr-CA"))

The leading #,0 part introduces the thousands separator, whilst the trailing .###… adds as many fractional digits as is required (up to a maximum corresponding to the number of # characters you specified). 开头的#,0部分引入了千位分隔符,而末尾的.###…添加了所需的任意小数位数(最多与您指定的#字符的数量相对应)。 The , and . ,. specifiers are not used verbatim, but interpreted according to the current culture. 说明符不是逐字使用的,而是根据当前的文化来解释的。

Refer to http://ideone.com/QypZBP for a sample run: 请参阅http://ideone.com/QypZBP以获取示例运行:

double d = 12345.6789;
Console.WriteLine(d.ToString("#,0.##########", new CultureInfo("fr-CA")));
// Output: "12 345,6789"

Edit : Since you've clarified that you're using a float , then the reason why the last digits are getting truncated is due to the limited precision of that data type. 编辑 :由于您已经澄清了使用的是float ,因此最后一位被截断的原因是由于该数据类型的精度有限。 Per MSDN, float has a precision of 7 digits, meaning that the last two digits are never even stored in your variable. 根据MSDN, float的精度为7位数字,这意味着最后两位数字甚至都不会存储在变量中。 If you want to be able to represent your number, you should change your data type to double . 如果您希望能够代表数字,则应将数据类型更改为double

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM