[英]custom format for decimal c#
I'm trying to format a decimal value with decimals to a custom format without comas or points , Having checking http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx but can't find the format I need 我正在尝试将带小数的十进制值格式化为没有逗号或点的自定义格式,检查http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx但找不到我需要的格式
I need to convert a decimal value for example 3.1416 to 314 or even better 0000000314, any clue? 我需要将例如3.1416的十进制值转换为314甚至更好的0000000314,任何线索?
要缩放100并显示最多9个前导零使用
String.Format("{0:0000000000}", (value * 100));
仅供展示
String.Format("{0:##########}", (value * 100))
Make a simple Method 制作一个简单的方法
public static string FormatNumberMultipliedByOneHundred(string inputString)
{
inputString = string.Format("{0:########}", (inputString * 100));
return inputString;
}
I guess the best way to solve this issue is using ValueConverters. 我想解决这个问题的最好方法是使用ValueConverters。 With a few easy steps you can write an ValueConverter that takes an arbitrary object as input applies some conversion and outputs the result.
通过几个简单的步骤,您可以编写一个ValueConverter,它接受任意对象作为输入应用一些转换并输出结果。
These ValueConverters are highly efficient and in case you write one converter for one particular conversion (take care of high cohesion) they are very handy and re-usable 这些ValueConverters非常高效,如果您为一次特定的转换编写一个转换器(注意高内聚),它们非常方便且可重复使用
What you need is the IValueConverter interafce which you must implement in your Converter class. 你需要的是你必须在Converter类中实现的IValueConverter交互。 A conversion always converts some A to some B. So the interface contains exactly two methods that are responsible for converting in one direction and for converting back (the opposite direction)
转换总是将一些A转换为某些B.因此,接口包含两个方法,它们负责在一个方向上转换并转换回(相反的方向)
It's good practice to write a general base class that all your converters can inherit: 编写一个所有转换器都可以继承的通用基类是一种很好的做法:
public class ValueConverterBase : IValueConverter {
public virtual object Convert (object value, Type convertTargetType, object convertParameter, System.Globalization.CultureInfo convertCulture) {
return value;
}
public virtual object ConvertBack (object value, Type convertBackTargetType, object convertBackParameter, System.Globalization.CultureInfo convertBackCulture) {
return value;
}
}
Then you can write your converter classes that actually implement the conversion code: 然后,您可以编写实际实现转换代码的转换器类:
public class NumberConverter : ValueConverterBase {
public override object Convert (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
// code for converting
}
public override object ConvertBack (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
// code for converting back
}
}
You can find plenty of documentation and tutorials on ValueConverter on the internet. 您可以在互联网上找到有关ValueConverter的大量文档和教程。
Hope this helps :) 希望这可以帮助 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.