Reputation: 51
I'm looking to String.Format
or ToString()
a decimal
to get just the fraction part.
'123.56m => "56"
Upvotes: 1
Views: 789
Reputation: 155698
.Substring
call too.CultureInfo.InvariantCulture
, otherwise per-user language/culture/region settings will mean possibly different default formatting and radix-point (decimal-point) chars will be used, but using CultureInfo.InvariantCulture
means the radix-point will always be an ASCII dot '.'
char.IFormatProvider
and ICustomFormatter
, unfortunately Decimal.ToString(IFormatProvider)
requires provider
.GetFormat to return a NumberFormatInfo
value and doesn't support using ICustomFormatter
.
IFormatProvider
and ICustomFormatter
will work if used with String.Format
, but that's not in your question.Math.Abs()
to convert negative numbers to positive numbers (otherwise there's a leading -
sign char which would complicate the .Substring
call-site).".############################"
as the format-string corresponds to the maximum number of decimal places in a Decimal
value (i.e. 28 digits).
.
part is necessary.fractionalPart
is 0
then there are no digits to render and .ToString( format: "#.########", provider: CultureInfo.InvariantCulture )
will return ""
(which will cause .Substring( startIndex: 1 )
to throw an exception, so we can skip that with a ternary expression).Decimal dec = 123.45M;
Decimal fractionalPart = Math.Abs( dec - Decimal.Truncate( dec ) );
String formatted = fractionalPart.ToString( CultureInfo.InvariantCulture, ".############################" );
if( formatted.StartsWith( '.' ) ) formatted = formatted.Substring( startIndex: 1 );
Console.WriteLine( "{0} --> \"{1}\"", dec, formatted ); // "123.45 --> 45"
You can simplify it down to 2 lines:
Decimal fractionalPart = Math.Abs( value - Decimal.Truncate( value ) );
return ( fractionalPart == 0 ) ? String.Empty : fractionalPart.ToString( format: ".############################", provider: CultureInfo.InvariantCulture ).Substring( startIndex: 1 );
Examples:
static String FmtOnlyFractional( Decimal value )
{
Decimal fractionalPart = Math.Abs( value - Decimal.Truncate( value ) );
return ( fractionalPart == 0 ) ? String.Empty : fractionalPart.ToString( format: ".############################", provider: CultureInfo.InvariantCulture ).Substring( startIndex: 1 );
}
FmtOnlyFractional( 0.0M ); => ""
FmtOnlyFractional( 1.0M ); => ""
FmtOnlyFractional( 1.1M ); => "1"
FmtOnlyFractional4( 1.0010000M ); => "001"
FmtOnlyFractional( 0.00000001M ); => "00000001"
FmtOnlyFractional( 123.00000001M ); => "00000001"
FmtOnlyFractional( -123.00000001M ); => "00000001"
FmtOnlyFractional( -0.00000001M ); => "00000001"
FmtOnlyFractional( -1.0000000M ); => ""
FmtOnlyFractional( -1.0000001M ); => "000001"
FmtOnlyFractional( -1.1000001M ); => "100001"
Upvotes: 6
Reputation: 46005
Another approach assuming you have up to 2 digits fraction
decimal value = 123.56m;
string result = (value % 1 * 100).ToString("F0"); //"56"
string Split()
approach
decimal value = 123.56m;
char separator = NumberFormatInfo.CurrentInfo.CurrencyDecimalSeparator.First();
string result = value.ToString("F2").Split(separator ).Last();
Upvotes: 2