简体   繁体   English

如何始终在逗号后显示三位数字

[英]How to always show three digits after the comma

Lets say i have a TextBox.可以说我有一个文本框。 When i write 0.5 i want it to Show it as 0.500 automatically.当我写 0.5 时,我希望它自动显示为 0.500。

My Problem is that as soon as the Digits after the comma are 0 they get cut off.我的问题是,一旦逗号后面的数字为 0,它们就会被截断。

Example例子

0.5 -> 0.500 0.5 -> 0.500

0.51 -> 0.510 0.51 -> 0.510

The Change itself will happen in the LostFocus Event (Excel like calculation).更改本身将发生在 LostFocus 事件中(类似 Excel 的计算)。

I Need fixed 3 Digits.我需要固定 3 位数。

In short:简而言之:

 var output = string.Format("{0:#,0.000}", value));   

Ex:前任:

 float[] values = new float[] { 0.5f, 0.51f, 0.51234f };
 foreach( var v in values)
      Console.WriteLine(string.Format("{0:#,0.000}", v));            
 Console.ReadKey();

在此处输入图像描述

if you are in WPF, you can do it in XAML:如果您在 WPF 中,您可以在 XAML 中进行操作:

<TextBlock Text="{Binding Path=Length, StringFormat={}{0:#000}}" TextAlignment="Right"/>

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

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