perl: simple question on string append


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl: simple question on string append
# 1  
Old 05-14-2007
perl: simple question on string append

I want to append a decimal number to a string. But I want to restrict the number to only 2 decimal points

for e.g:

my $output = "\n The number is = ";
my $number = 2.3333333;

$output = $output . $number;

But I want the $output as: "The number is = 2.33"; and not 2.3333333

(I do not want to print $output right now.. I just want it to have the number with only 2 decimal points)

can anyone please helpout..

Thanks !!!!!!!!!!!!
# 2  
Old 05-14-2007
Code:
$result = sprintf("%.2f", $number);

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl String Replacement Syntax Question . . .

Greetings! I've been tooling about with Perl to make a few string replacements in some files; and seem to have run into a bit of a squeeze :) Beginning with a simple text file, test.txt, we have the following content to be worked:Now, not wanting to have anyone feel left out, I decided that... (6 Replies)
Discussion started by: LinQ
6 Replies

2. Shell Programming and Scripting

Simple Perl question

Hello, I'm completely new to Perl and I'm just looking for a quick answer to some code I'm trying to come up with. I'm trying to access a website, part of the URL I want the user to be able to define via standard input. As you can see below I'm still trying to get the syntax. ... (2 Replies)
Discussion started by: wxornot
2 Replies

3. Red Hat

Syslog.conf: looking for a simple answer on a simple question

Cheers! In /etc/syslog.conf, if an error type is not specified, is it logged anywhere (most preferable is it logged to /var/log/messages) or not? To be more precise I am interested in error and critical level messages. At default these errors are not specified in syslog.conf, and I need to... (6 Replies)
Discussion started by: dr1zzt3r
6 Replies

4. Shell Programming and Scripting

Help making simple perl or bash script to create a simple matrix

Hello all! This is my first post and I'm very new to programming. I would like help creating a simple perl or bash script that I will be using in my work as a junior bioinformatician. Essentially, I would like to take a tab-delimted or .csv text with 3 columns and write them to a "3D" matrix: ... (16 Replies)
Discussion started by: torchij
16 Replies

5. UNIX for Dummies Questions & Answers

Append a string on the next line after a pattern string is found

Right now, my code is: s/Secondary Ins./Secondary Ins.\ 1/g It's adding a 1 as soon as it finds Secondary Ins. Primary Ins.: MEDICARE B DMERC Secondary Ins. 1: CONTINENTAL LIFE INS What I really want to achieve is having a 1 added on the next line that contain "Secondary Ins." It... (4 Replies)
Discussion started by: newbeee
4 Replies

6. Shell Programming and Scripting

Simple Question about Reading file by Perl

Hi , I just write a simple function to read the file line by line. But when I run it it says out of memory. I am not sure about the root cause, Can someone help me out of this? :D #! /usr/bin/perl use strict; sub checkAPs{ my $NDPDir = "/home/eweiqqu/NCB/NDP_files/"; ... (1 Reply)
Discussion started by: Damon_Qu
1 Replies

7. Shell Programming and Scripting

how to append a string to next line in perl

hi all , i have a requirement like this.. this just a sample script... $ cat test.sh #!/bin/bash perl -e ' open(IN,"addrss"); open(out,">>addrss"); @newval; while (<IN>) { @col_val=split(/:/); if ($.==1) { for($i=0;$i<=$#col_val;$i++) { ... (2 Replies)
Discussion started by: tprayush
2 Replies

8. Shell Programming and Scripting

Simple perl question

I am totally new to perl. I am modifying someone else's script. I have the following output: # ./some-perlscript A B C D E B - E, is generated through the print command that I put in the script. I want to remove A, it seems it is generated automatically by a custom OS it is querying when... (3 Replies)
Discussion started by: streetfighter2
3 Replies

9. Shell Programming and Scripting

PERL: simple comparing arrays question

Hi there, i have been trying different methods and i wonder if somebody could explain to me how i would perform a comparison on two arrays for example my @array1 = ("gary" ,"peter", "paul"); my @array2 = ("gary" ,"peter", "joe"); I have two arrays above, and i want to something like this... (5 Replies)
Discussion started by: hcclnoodles
5 Replies

10. Programming

simple question on string concat

This is a simple question... char *str = NULL; int num = 0; scanf ("%d", &num); str = ??? I want str to point to the string num.txt For e.g: If user enters num = 5, str should point to "5.txt" How do I do that ? Thanks (2 Replies)
Discussion started by: the_learner
2 Replies
Login or Register to Ask a Question