Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I want a value 134.45678MHz in only MHz - can perl do it?

Status
Not open for further replies.

DJpennywhistle

Programmer
Jun 1, 2000
32
US
Thats it. I want a number to only 3 significant bits ie MHz. <p>Gordon Bell<br><a href=mailto:gordon.bell@xilinx.com>gordon.bell@xilinx.com</a><br><a href= > </a><br>
 
The function <FONT FACE=monospace><b>int()</font></b> will return just the integer portion (numbers in front of the decimal point) for you -- would that be sufficient or did you want rounding?<br> <p>Mike<br><a href=mailto:michael.j.lacey@ntlworld.com>michael.j.lacey@ntlworld.com</a><br><a href= Cargill's Corporate Web Site</a><br>Please -- Don't send me email questions without posting them in Tek-Tips as well. Better yet -- Post the question in Tek-Tips and send me a note saying "Have a look at so-and-so in the thingy forum would you?"
 
Print to a string with format control for the accuracy you want, then convert back to floating point using atof(), which returns double precision. <p>Octalman<br><a href=mailto: > </a><br><a href= > </a><br>
 
Oops.&nbsp;&nbsp;Wrong language. <p>Octalman<br><a href=mailto: > </a><br><a href= > </a><br>
 
INAPG (I'm not a Perl Guru), so maybe I can learn something here too.&nbsp;&nbsp;How about $val = $val * 1000; $temp = pack l, $val; $newval = (int $temp)/1000; ?&nbsp;&nbsp;I don't see any type conversions in the module library, but that may have changed. <p>Octalman<br><a href=mailto: > </a><br><a href= > </a><br>
 
As an alternative to Mike's suggestion, and to show Octal was right, even if he had the wrong language (;^), try this:<br><FONT FACE=monospace><br>#!/usr/bin/perl<br>$Value=134.45678;<br><br>$Mhz = sprintf(&quot;%3d&quot;, $Value);<br><br>print $Mhz, &quot;\n&quot;;<br></font><br><br>Note - this does not do any rounding, so 134.444 will be 134, as will 134.888.
 
Thanks - that has been a great help <p>Gordon Bell<br><a href=mailto:gordon.bell@xilinx.com>gordon.bell@xilinx.com</a><br><a href= > </a><br>
 
Thanks Andy - how did you get that postfix? <p>Gordon Bell<br><a href=mailto:gordon.bell@xilinx.com>gordon.bell@xilinx.com</a><br><a href= > </a><br>
 
The postfix on my member name? &quot;Bo&quot;?&nbsp;&nbsp;Abbreviation of my second name that's been used in other places, so it's easy for me to remember :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top