?
Solved

6.1E-5 into a real decimal number

Posted on 2011-10-18
4
Medium Priority
?
579 Views
Last Modified: 2012-05-12
When I do some math, a decimal number turns into something like this:
6.1E-5
0
Comment
Question by:hankknight
  • 3
4 Comments
 
LVL 3

Expert Comment

by:clamps
ID: 36985402
printf("%.15f", $yourfloatvariableorcalculationhere);
0
 
LVL 16

Author Comment

by:hankknight
ID: 36985454
How can I use that in my code below?
<?php

$log = trim('
10:56:46.344794 <= Recv header, 17 bytes (0x11)
10:56:46.344855 <= Recv header, 36 bytes (0x24)
10:56:46.344889 <= Recv header, 25 bytes (0x19)
');

convertLog($log);

function convertLog ($str) {
 $v=0;
 foreach(preg_split("/(\r?\n)/", $str) as $line){
    $vv=$v;
    $pos = strpos($line, ' ');
    $dotpos = 1+strpos($line, '.');
    $v = strtotime(substr($line,0,$pos)) . substr($line,$dotpos,$pos-$dotpos) * 0.000001;
    echo '<h1>'. round($v - $vv,6) . '</h1>';
 }

}

?>

Open in new window

0
 
LVL 3

Expert Comment

by:clamps
ID: 36985457
or just printf("%f", $yourfloatvariableorcalculationhere);
0
 
LVL 3

Accepted Solution

by:
clamps earned 2000 total points
ID: 36985469
like this?
$log = trim('
10:56:46.344794 <= Recv header, 17 bytes (0x11)
10:56:46.344855 <= Recv header, 36 bytes (0x24)
10:56:46.344889 <= Recv header, 25 bytes (0x19)
');

convertLog($log);

function convertLog ($str) {
 $v=0;
 foreach(preg_split("/(\r?\n)/", $str) as $line){
    $vv=$v;
    $pos = strpos($line, ' ');
    $dotpos = 1+strpos($line, '.');
    $v = strtotime(substr($line,0,$pos)) . substr($line,$dotpos,$pos-$dotpos) * 0.000001;
    echo '<h1>'; 
    printf("%f", round($v - $vv,6));
    echo '</h1>';
 }

}

Open in new window

0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month16 days, 11 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question