Reputation: 2083
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
I need to convert a decimal value for example 3.1416 to 314 or even better 0000000314, any clue?
Upvotes: 3
Views: 1525
Reputation: 18843
Make a simple Method
public static string FormatNumberMultipliedByOneHundred(string inputString)
{
inputString = string.Format("{0:########}", (inputString * 100));
return inputString;
}
Upvotes: 1
Reputation: 5886
I guess the best way to solve this issue is using 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.
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
What you need is the IValueConverter interafce which you must implement in your Converter class. 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)
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.
Hope this helps :)
Upvotes: 0
Reputation: 152511
To scale by 100 and show up to 9 leading zeros use
String.Format("{0:0000000000}", (value * 100));
Upvotes: 4