Decimal.Truncate(Decimal) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
返回指定的 Decimal整型数字;任何小数位将被丢弃。
public:
static System::Decimal Truncate(System::Decimal d);
public:
static System::Decimal Truncate(System::Decimal d) = System::Numerics::IFloatingPoint<System::Decimal>::Truncate;
public static decimal Truncate(decimal d);
static member Truncate : decimal -> decimal
Public Shared Function Truncate (d As Decimal) As Decimal
参数
- d
- Decimal
要截断的十进制数。
返回
舍入为零的结果 d ,舍入为最接近的整数。
实现
示例
下面的代码示例使用 Truncate 该方法放弃多个 Decimal 值的小数位数。
// Example of the decimal.Negate, decimal.Floor, and decimal.Truncate
// methods.
using System;
class DecimalFloorNegTruncDemo
{
const string dataFmt = "{0,-30}{1,26}";
// Display decimal parameters and the method results.
public static void ShowDecimalFloorNegTrunc( decimal Argument )
{
Console.WriteLine( );
Console.WriteLine( dataFmt, "decimal Argument", Argument );
Console.WriteLine( dataFmt, "decimal.Negate( Argument )",
decimal.Negate( Argument ) );
Console.WriteLine( dataFmt, "decimal.Floor( Argument )",
decimal.Floor( Argument ) );
Console.WriteLine( dataFmt, "decimal.Truncate( Argument )",
decimal.Truncate( Argument ) );
}
public static void Main( )
{
Console.WriteLine( "This example of the \n" +
" decimal.Negate( decimal ), \n" +
" decimal.Floor( decimal ), and \n" +
" decimal.Truncate( decimal ) \n" +
"methods generates the following output." );
// Create pairs of decimal objects.
ShowDecimalFloorNegTrunc( 0M );
ShowDecimalFloorNegTrunc( 123.456M );
ShowDecimalFloorNegTrunc( -123.456M );
ShowDecimalFloorNegTrunc(
new decimal( 1230000000, 0, 0, true, 7 ) );
ShowDecimalFloorNegTrunc( -9999999999.9999999999M );
}
}
/*
This example of the
decimal.Negate( decimal ),
decimal.Floor( decimal ), and
decimal.Truncate( decimal )
methods generates the following output.
decimal Argument 0
decimal.Negate( Argument ) 0
decimal.Floor( Argument ) 0
decimal.Truncate( Argument ) 0
decimal Argument 123.456
decimal.Negate( Argument ) -123.456
decimal.Floor( Argument ) 123
decimal.Truncate( Argument ) 123
decimal Argument -123.456
decimal.Negate( Argument ) 123.456
decimal.Floor( Argument ) -124
decimal.Truncate( Argument ) -123
decimal Argument -123.0000000
decimal.Negate( Argument ) 123.0000000
decimal.Floor( Argument ) -123
decimal.Truncate( Argument ) -123
decimal Argument -9999999999.9999999999
decimal.Negate( Argument ) 9999999999.9999999999
decimal.Floor( Argument ) -10000000000
decimal.Truncate( Argument ) -9999999999
*/
// Example of the decimal.Negate, decimal.Floor, and decimal.Truncate
// methods.
open System
let dataFmt obj1 obj2 = printfn $"{obj1,-30}{obj2,26}"
// Display decimal parameters and the method results.
let showDecimalFloorNegTrunc (argument: decimal) =
printfn ""
dataFmt "decimal argument" argument
dataFmt "Decimal.Negate( argument )" (Decimal.Negate argument)
dataFmt "decimal.Floor( argument )" (Decimal.Floor argument)
dataFmt "decimal.Truncate( argument )" (Decimal.Truncate argument)
printfn
"""This example of the
decimal.Negate( decimal ),
decimal.Floor( decimal ), and
decimal.Truncate( decimal )
methods generates the following output."""
// Create pairs of decimal objects.
showDecimalFloorNegTrunc 0M
showDecimalFloorNegTrunc 123.456M
showDecimalFloorNegTrunc -123.456M
showDecimalFloorNegTrunc (Decimal(1230000000, 0, 0, true, 7uy))
showDecimalFloorNegTrunc -9999999999.9999999999M
// This example of the
// decimal.Negate( decimal ),
// decimal.Floor( decimal ), and
// decimal.Truncate( decimal )
// methods generates the following output.
//
// decimal argument 0
// Decimal.Negate( argument ) 0
// decimal.Floor( argument ) 0
// decimal.Truncate( argument ) 0
//
// decimal argument 123.456
// Decimal.Negate( argument ) -123.456
// decimal.Floor( argument ) 123
// decimal.Truncate( argument ) 123
//
// decimal argument -123.456
// Decimal.Negate( argument ) 123.456
// decimal.Floor( argument ) -124
// decimal.Truncate( argument ) -123
//
// decimal argument -123.0000000
// Decimal.Negate( argument ) 123.0000000
// decimal.Floor( argument ) -123
// decimal.Truncate( argument ) -123
//
// decimal argument -9999999999.9999999999
// Decimal.Negate( argument ) 9999999999.9999999999
// decimal.Floor( argument ) -10000000000
// decimal.Truncate( argument ) -9999999999
' Example of the Decimal.Negate, Decimal.Floor, and Decimal.Truncate
' methods.
Module DecimalFloorNegTruncDemo
Const dataFmt As String = "{0,-30}{1,26}"
' Display Decimal parameters and their product, quotient, and
' remainder.
Sub ShowDecimalFloorNegTrunc( Argument as Decimal )
Console.WriteLine( )
Console.WriteLine( dataFmt, "Decimal Argument", Argument )
Console.WriteLine( dataFmt, _
"Decimal.Negate( Argument )", _
Decimal.Negate( Argument ) )
Console.WriteLine( dataFmt, _
"Decimal.Floor( Argument )", _
Decimal.Floor( Argument ) )
Console.WriteLine( dataFmt, _
"Decimal.Truncate( Argument )", _
Decimal.Truncate( Argument ) )
End Sub
Sub Main( )
Console.WriteLine( "This example of the " & vbCrLf & _
" Decimal.Negate( Decimal ), " & vbCrLf & _
" Decimal.Floor( Decimal ), and " & vbCrLf & _
" Decimal.Truncate( Decimal ) " & vbCrLf & _
"methods generates the following output." )
' Create pairs of Decimal objects.
ShowDecimalFloorNegTrunc( 0D )
ShowDecimalFloorNegTrunc( 123.456D )
ShowDecimalFloorNegTrunc( -123.456D )
ShowDecimalFloorNegTrunc( _
new Decimal( 1230000000, 0, 0, True, 7 ) )
ShowDecimalFloorNegTrunc( -9999999999.9999999999D )
End Sub
End Module
' This example of the
' Decimal.Negate( Decimal ),
' Decimal.Floor( Decimal ), and
' Decimal.Truncate( Decimal )
' methods generates the following output.
'
' Decimal Argument 0
' Decimal.Negate( Argument ) 0
' Decimal.Floor( Argument ) 0
' Decimal.Truncate( Argument ) 0
'
' Decimal Argument 123.456
' Decimal.Negate( Argument ) -123.456
' Decimal.Floor( Argument ) 123
' Decimal.Truncate( Argument ) 123
'
' Decimal Argument -123.456
' Decimal.Negate( Argument ) 123.456
' Decimal.Floor( Argument ) -124
' Decimal.Truncate( Argument ) -123
'
' Decimal Argument -123.0000000
' Decimal.Negate( Argument ) 123.0000000
' Decimal.Floor( Argument ) -123
' Decimal.Truncate( Argument ) -123
'
' Decimal Argument -9999999999.9999999999
' Decimal.Negate( Argument ) 9999999999.9999999999
' Decimal.Floor( Argument ) -10000000000
' Decimal.Truncate( Argument ) -9999999999
注解
此方法 d 向零舍入到最接近的整数,对应于在小数点之后丢弃任何数字。