PhpDig.net

What is PhpDig?
PhpDig is a PHP MySQL based
Web Spider & Search Engine.




doubleval

Name

doubleval — Converts a value to a double.

Synopsis

double doubleval(value);
mixed value: Scalar value to convert to a double

Returns

double

Description

doubleval() attempts to convert a single scalar value to a double. A double is a floating-point number such as 1.3 or 0.129.

The function can convert any normal numeric value, as well as numbers that are represented in scientific notation (such as 1.879e4), as strings (such as "1.98e7 is 1.98 times 10 to the power of 7" or "1,000 hairy monkeys"), in octal base notation (016 or 0377), or in hexadecimal base notation (0xFF or 0xDE004).

Note

The maximum length of a floating-point number is platform-dependent. A common size for many platforms is the IEEE 64-bit floating-point format - approximately 1.8 x 10308 with a precision of about 14 digits.

Warning

The internal format used by most computers to represent floating-point numbers is inherently flawed - making the number lose tiny amounts of precision. The amount of precision lost is generally only a problem in situations that require high precision. If you need higher precision, use the arbitrary-precision mathematics (BC) functions.

Also, some fractions (such as 1/9 and 22/7) cannot be represented in a finite number of digits. In cases like these, the number is truncated to the maximum precision that can be displayed.

Example 1374. Show some precision errors

<pre>
<?php
// Store as many digits as possible from 1/11 and 1/9
$val_1 = 1/9;
$val_2 = 1/11;

// Note the loss of precision
print "$val_1\n";
print "$val_2\n\n";

// Try to convert the fractions back to a whole number
$value = $val_1 * $val_2 * 9 * 11;

// Display the value - should be 1
print $value . "\n";

// Use floor() to round down the value - should return 0
// This happens because the number is actually something like 0.999999999999999
// ...however this goes beyond the precision available for PHP and is simply displayed as 1
// When this value is passed to floor(), the fractional remainder is dropped, leaving 0
print floor ($value) . "\n";

// Now try the same test with a different order of operations
// The result should be 1
print floor ($val_1 * 9 * $val_2 * 11) . "\n";

?>
</pre>

See also

To convert a value to a double:

printf()

sprintf()

settype()

Typecasting (see the PHP Manual)

To find whether a value is a double:

is_double()

gettype()

Example

Example 1375. Convert a value to a double

$value = doubleval (10);

// Do the same thing using typecasting
$value = (double) 10;

Example 1376. Convert a bunch of values to doubles

<pre>
<?php
// Make a list of the values that we want to convert
$values = array (
    1000 => 1000, '0xFF' => 0xFF, '033' => 033, 'foo' => 'foo', '10,000' => '10,000',
    '1.23456789e10' => 1.23456789e10, '1.22e30' => 1.22e30, '127.0.0.1' => '127.0.0.1'
);

// Loop through the values
// Show what they look like before and after conversion
// Pay close attention to the 'foo', '10,000', '1.22e30', and '127.0.0.1' values
// Notice that only the parts that are recognizable as normal numbers are converted.
// With '1.22e30', note how there may be an error in the value of the number.
// Interestingly, this is due to printf - not to floating-point value errors
// ... - see the above warning for details
foreach ($values as $key => $value)
    printf ("<b>%-20s</b> %f\n", "'$key'", doubleval ($value));

?>
</pre>



PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing (Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch). This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/). The authors of this book have elected not to choose any options under the OPL. This online book was obtained from http://www.fooassociates.com/phpfer/ and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4 for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All persons and entities shall have neither liability nor responsibility to any person or entity with respect to any loss or damage arising from the information contained in this book.

Powered by: vBulletin Version 3.0.7
Copyright ©2000 - 2005, Jelsoft Enterprises Ltd.
Copyright © 2001 - 2005, ThinkDing LLC. All Rights Reserved.