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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

trunc an int

Status
Not open for further replies.

TyzA

Programmer
Jan 7, 2002
86
BE
Hi,

Can anybody tell me how I can truncate an int.
I use now this:
sprintf "%.0f", ($num_records / $num_rows);

If I have a value like 17.6 I will always get 18. If i have 17.4, I'll get 17. So it rounds.

Is there a way that I will just get 17, no mather what the decimal is.

Thanks for any response.

Tijs Programming is like sex: one mistake and you have to support it for the rest of your life.
 
Hi again,

sorry, but I made a mistake. Instead of getting 17 I want to get 18. So it must always round up, even when you have somthing like 17.3

Thanks again.

Tijs Programming is like sex: one mistake and you have to support it for the rest of your life.
 
I think that will always round up, even if records/rows happened to be an int. This might be a little closer.

a generic treatment...
Code:
#!perl
$in = $ARGV[0] || die "No input from command line.\n";

$num = ($in =~ /\.\d/) ? int($in + 1) : $in ;
print "NUM: $num\n";
'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Thanks guys,

I've tried them both, but only the first one seems to work.

Thanks again,

Tijs Programming is like sex: one mistake and you have to support it for the rest of your life.
 
goBoating is correct, if $num_rows evenly divides $num_records then you will still get the next highest integer (for example if $num_records = 85 and $num_rows = 5 then you will get 18 as the rounded value even though $num_records/$num_rows == 17. Perhaps you didn't implement it correctl. This worked for me
Code:
my $in = $num_records / $num_rows;
my $num = ($in =~ /\.\d/) ? int($in + 1) : $in ;

jaa
 
Thanks justice41

It works also like that now and goboating was right about the rounding.

Tijs Programming is like sex: one mistake and you have to support it for the rest of your life.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top