Double Class

Contains methods for the Double primitive data type.

Namespace

System

Usage

For more information on Double, see Double Data Type.

Double Methods

The following are methods for Double.

format()

Returns the String value for this Double using the locale of the context user

Signature

public String format()

Return Value

Type: String

Example

1Double myDouble = 1261992;
2system.assertEquals('1,261,992', myDouble.format());

intValue()

Returns the Integer value of this Double by casting it to an Integer.

Signature

public Integer intValue()

Return Value

Type: Integer

Example

1Double DD1 = double.valueOf('3.14159');
2Integer value = DD1.intValue();
3system.assertEquals(value, 3);

longValue()

Returns the Long value of this Double.

Signature

public Long longValue()

Return Value

Type: Long

Example

1Double myDouble = 421994;
2Long value = myDouble.longValue();
3System.assertEquals(421994, value);

round()

Returns the closest Long to this Double value.

Signature

public Long round()

Return Value

Type: Long

Example

1Double D1 = 4.5;
2Long L1 = D1.round();
3System.assertEquals(5, L1);
4
5Double D2= 4.2;
6Long L2= D2.round();
7System.assertEquals(4, L2);
8
9Double D3= -4.7;
10Long L3= D3.round();
11System.assertEquals(-5, L3);

valueOf(stringToDouble)

Returns a Double that contains the value of the specified String. As in Java, the String is interpreted as representing a signed decimal.

Signature

public static Double valueOf(String stringToDouble)

Parameters

stringToDouble
Type: String

Return Value

Type: Double

Example

1Double DD1 = double.valueOf('3.14159');

valueOf(fieldValue)

Converts the specified object to a Double value. Use this method to convert a history tracking field value or an object that represents a Double value.

Signature

public static Double valueOf(Object fieldValue)

Parameters

fieldValue
Type: Object

Return Value

Type: Double

Usage

Use this method with the OldValue or NewValue fields of history sObjects, such as AccountHistory, when the field type corresponds to a Double type, like a number field.

Example

1List<AccountHistory> ahlist = 
2  [SELECT Field,OldValue,NewValue
3   FROM AccountHistory];
4for(AccountHistory ah : ahlist) {
5  System.debug('Field: ' + ah.Field);
6  if (ah.field == 'NumberOfEmployees') {
7    Double oldValue = 
8      Double.valueOf(ah.OldValue);
9    Double newValue = 
10      Double.valueOf(ah.NewValue);
11}