How do I convert strings to floating number?

Hi there,

I have a simple problem that I'm sure has been asked before (so why not
again ;))

I have a list like this:

1e-100
2e-100
1e-45
5e-10
1
10
20

and want to make correct assignments:

1e-100	SMALL
2e-100	SMALL
1e-45	BIGGER
5e-10	BIGGER
1	BIGGER
10	BIG
20	BIG

But with this code

while (<>) {
    chomp;
    if ($_ lt 1e-50) {
        print "$_\t SMALL\n";
    } elsif (($_ gt 1e-50) && $_ lt 5) {
        print "$_\t BIGGER\n";
    } elsif ($_ gt 5) {
        print "$_\t BIGGEST\n";
    }

I only get 

1e-100   SMALL
2e-100   BIGGER
1e-45    SMALL
5e-10    BIGGEST
1        SMALL
10       SMALL
20       BIGGER

which is not matematically correct. How do convert "floating strings" to
floating number? I thought "lt" would be the right one to choose...

Thanks in advance!
Marcus 

0
m
10/14/2003 11:24:26 AM
perl.beginners 29360 articles. 3 followers. Follow

6 Replies
382 Views

Similar Articles

[PageSpeed] 14

Marcus Claesson wrote:

> Hi there,
>
> I have a simple problem that I'm sure has been asked before (so why not
> again ;))
>
> I have a list like this:
>
> 1e-100
> 2e-100
> 1e-45
> 5e-10
> 1
> 10
> 20
>
> and want to make correct assignments:
>
> 1e-100  SMALL
> 2e-100  SMALL
> 1e-45   BIGGER
> 5e-10   BIGGER
> 1       BIGGER
> 10      BIG
> 20      BIG
>
> But with this code
>
> while (<>) {
>     chomp;
>     if ($_ lt 1e-50) {
>         print "$_\t SMALL\n";
>     } elsif (($_ gt 1e-50) && $_ lt 5) {
>         print "$_\t BIGGER\n";
>     } elsif ($_ gt 5) {
>         print "$_\t BIGGEST\n";
>     }

lt, gt etc. are used for string comparisons. Change 'lt' to < and 'gt' to >
and your code should work.


>
>
> I only get
>
> 1e-100   SMALL
> 2e-100   BIGGER
> 1e-45    SMALL
> 5e-10    BIGGEST
> 1        SMALL
> 10       SMALL
> 20       BIGGER
>
> which is not matematically correct. How do convert "floating strings" to
> floating number? I thought "lt" would be the right one to choose...
>
> Thanks in advance!
> Marcus

0
sraghavan
10/14/2003 11:44:05 AM
> lt, gt etc. are used for string comparisons. Change 'lt' to < and 'gt' to >
> and your code should work.
> 

You're right, but this script was a simplification of a bigger one where
'>' didn't work (my mistake to not spot the difference...). Here is a
more 'real' situation:

Input file:
4635a   1e-180  BL0976
4635a   0       BL0978
4635a   1e-10   BL1343
4635a   8e-86   BL1774
4635a   6e-18   Blon1206
4635a   1e-123  BL1165
4635a   5       BL0978
4635a   50      BL0978

Script:

while (<>) {
    @row = (split /\t/);
    ($contig,$E_value,$locus_tag) = (@row);
    if ($E_value < 1e-50) {
        print "$E_value\t SMALL\n";
    } elsif (($E_value > 1e-50) && $E_value < 5) {
        print "$E_value\t BIGGER\n";
    } elsif ($_ > 5) {
        print "$E_value\t BIGGEST\n";
    }
}

And I then get this:
1e-180   SMALL
0        SMALL
1e-10    BIGGER
8e-86    SMALL
6e-18    BIGGER
1e-123   SMALL
Argument "\x{34}\x{36}..." isn't numeric in numeric gt (>) at
/home/marcus/Scripts/string2num.pl line 7, <> line 7.
5        BIGGEST
Argument "\x{34}\x{36}..." isn't numeric in numeric gt (>) at
/home/marcus/Scripts/string2num.pl line 7, <> line 8.
50       BIGGEST

Thus, the operator isn't always correct and is not very accepted by perl

Do you know the error?

Marcus


0
m
10/14/2003 12:04:26 PM
Marcus Claesson said:
> Hi there,
>
> I have a simple problem that I'm sure has been asked before (so why not
> again ;))
>
> I have a list like this:
>
> 1e-100
> 2e-100
> 1e-45
> 5e-10
> 1
> 10
> 20
>
> and want to make correct assignments:
>
> 1e-100	SMALL
> 2e-100	SMALL
> 1e-45	BIGGER
> 5e-10	BIGGER
> 1	BIGGER
> 10	BIG
> 20	BIG
>
> But with this code
>
> while (<>) {
>     chomp;
>     if ($_ lt 1e-50) {
>         print "$_\t SMALL\n";
>     } elsif (($_ gt 1e-50) && $_ lt 5) {
>         print "$_\t BIGGER\n";
>     } elsif ($_ gt 5) {
>         print "$_\t BIGGEST\n";
>     }
>
> I only get
>
> 1e-100   SMALL
> 2e-100   BIGGER
> 1e-45    SMALL
> 5e-10    BIGGEST
> 1        SMALL
> 10       SMALL
> 20       BIGGER
>
> which is not matematically correct. How do convert "floating strings" to
> floating number? I thought "lt" would be the right one to choose...

You don't need to.  If you want it to be a number, use it like one.  That
means using < and > instead of lt and gt.

You'll also probably want <= or >= at some points in case you get 1e-50
and 5 as input.

-- 
Paul Johnson - paul@pjcj.net
http://www.pjcj.net

0
paul
10/14/2003 12:05:16 PM
Marcus Claesson wrote:
> > lt, gt etc. are used for string comparisons. Change 'lt' to < and
> > 'gt' to > and your code should work. 
> > 
> 
> You're right, but this script was a simplification of a
> bigger one where
> '>' didn't work (my mistake to not spot the difference...). Here is a
> more 'real' situation: 
> 
> Input file:
> 4635a   1e-180  BL0976
> 4635a   0       BL0978
> 4635a   1e-10   BL1343
> 4635a   8e-86   BL1774
> 4635a   6e-18   Blon1206
> 4635a   1e-123  BL1165
> 4635a   5       BL0978
> 4635a   50      BL0978
> 
> Script:
> 
> while (<>) {
>     @row = (split /\t/);
>     ($contig,$E_value,$locus_tag) = (@row);
>     if ($E_value < 1e-50) {
>         print "$E_value\t SMALL\n";
>     } elsif (($E_value > 1e-50) && $E_value < 5) {
>         print "$E_value\t BIGGER\n";
>     } elsif ($_ > 5) {

This should be referencing $E_value, not $_.

>         print "$E_value\t BIGGEST\n";
>     }
> }

This whole thing should be simplified:

    while(<>) {
        my ($contig,$E_value,$locus_tag) = split;
        if ($E_value < 1e-50) {
            print "$E_value\t SMALL\n";
        } elsif ($E_value < 5) {
            print "$E_value\t BIGGER\n";
        } else {
            print "$E_value\t BIGGEST\n";
        }
    }

> 
> And I then get this:
> 1e-180   SMALL
> 0        SMALL
> 1e-10    BIGGER
> 8e-86    SMALL
> 6e-18    BIGGER
> 1e-123   SMALL
> Argument "\x{34}\x{36}..." isn't numeric in numeric gt (>) at
> /home/marcus/Scripts/string2num.pl line 7, <> line 7. 5        BIGGEST
> Argument "\x{34}\x{36}..." isn't numeric in numeric gt (>) at
> /home/marcus/Scripts/string2num.pl line 7, <> line 8. 50       BIGGEST
> 
> Thus, the operator isn't always correct and is not very
> accepted by perl

The operator is correct. Your program has a bug.
0
Bob_Showalter
10/14/2003 1:13:14 PM
Marcus Claesson wrote:

> > lt, gt etc. are used for string comparisons. Change 'lt' to < and 'gt' to >
> > and your code should work.
> >
>
> You're right, but this script was a simplification of a bigger one where
> '>' didn't work (my mistake to not spot the difference...). Here is a
> more 'real' situation:
>
> Input file:
> 4635a   1e-180  BL0976
> 4635a   0       BL0978
> 4635a   1e-10   BL1343
> 4635a   8e-86   BL1774
> 4635a   6e-18   Blon1206
> 4635a   1e-123  BL1165
> 4635a   5       BL0978
> 4635a   50      BL0978
>
> Script:
>
> while (<>) {
>     @row = (split /\t/);
>     ($contig,$E_value,$locus_tag) = (@row);
>     if ($E_value < 1e-50) {
>         print "$E_value\t SMALL\n";
>     } elsif (($E_value > 1e-50) && $E_value < 5) {
>         print "$E_value\t BIGGER\n";
>     } elsif ($_ > 5) {

I think you meant elsif ($E_value > 5) {
You might also want to add a >= or <= somewhere to process 5 or 1e-50

>
>         print "$E_value\t BIGGEST\n";
>     }
> }
>

0
sraghavan
10/14/2003 1:15:22 PM
>>>>> "Marcus" == Marcus Claesson <m.claesson@student.ucc.ie> writes:

Marcus> while (<>) {
Marcus>     chomp;
Marcus>     if ($_ lt 1e-50) {

Don't use "lt" for numbers.  Use "<".

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
0
merlyn
10/14/2003 3:02:45 PM
Reply:

Similar Artilces:

convert string to number
Hello, if I use the sprintf function it would give me a number STRING, but = not number, so I have to trick the string to become number by adding and su= btracting the number with 1. Is there a better way than this ? I want it to= be a number data type and not string.=0A=0Ae.g =0A=0Amy $strNumber =3D spr= intf("%04d", 123);=0Amy $number =3D $strNumber + 1 - 1; # to trick Perl to = convert to number datatype instead of string. =0A=0A=0AThanks,=0A=0AWilliam= =0A=0ASend instant messages to your online friends http://uk.messenger.yaho= o.com On Sun, Jun 15, 2008 at 8:09 PM, Wi...

Convert a string to number
Hi. I want to set a value such as 1.5 or 10 to a text box in a report datawindow at the run time. The value will come from the user before he/she runs the report. How do I convert this string in the text box to a number that will be used for further computation at the summary band? I tried number() function, but it requires a real string and refuses to accept a variable or a column name. Thanks in advance for any help. Jian Kuang If the value comes from the user, it would be in the form of a Retrieve Argument. You can refer to the retrieve argument in the computed field for...

convert string to number?
Hi, I have a problem where a number read from a file is being treated as a string by perl (I think!). I am using the module RSPerl which is an interface between Perl and the statistical language R. When i read a column of numbers from a file and pass it to the perl/R function i get an "invalid 'type' (character) of argument" error. The reason i think this is a problem on the Perl side is shown by this pseudocode: while(<FILE>){ ... stuff to extract $value from each row ... push(@list1, $value); } @list2 = (100.2, 232.333, 344.2); # these ...

Convert String to Number
I am running PB 7.03 build 10047 on Windows 2000. How do I convert a string value to number in the DataWindow script? I can convert number to string using String(ll_value), but how do I do the reverse? The help file says I should be able to use Number(ls_value), but the error checker tells me that is an unknown function. Thanks for your help. dec('123') or long('123') or integer('123') -- Kim Berghall Sisu Group, Inc. remove no_spam. no_spam.kberghall@sisugrp.com www.sisugrp.com <Scott_Emery> wrote in message news:72D1C6C14C517376005C20...

convert string to float??
Simple question. How to convert string to float or double?? Double.Parse(StringName);RTFM - straight talk for web developers. Unmoderated, uncensored, occasionally unreadableJason Brown - MVP, IIS...

Converting a string to a number
Hello everyone, first post and all that, i got a bit of a problem. First off, the DB information: id: autonumber score: text Both these feilds in the database are numerical, however, the score is used as a text feild, the id an autonumber. When i use a querystring to pass the score over to another page, so that the page produces only information with that perticular score matching, it works. When i try to use the ID number, i get a case miss match. I can only fathom that this is because its an autonumber. However, i need to keep the DB design as it is, as when i add a ne...

Convertion numbers to strings
Hello! I have some numbers in data type "Numeric". The task is to write expression wich combines prefix, number and suffix. The number must have no spare zero's on tale. Linke in example: "NDS 20%" "SN 1.5%" "ANT 0.25%" The slyboot server makes all the numbers with the same length of fract. part. like "20.00000" and I couldn't cope with this for two weeks already. I don't know of a way to do that in SQL Anywhere. -- Jim Egan [TeamPS] Dealer Solutions, Inc. Houston, TX See you at the Powersoft User Confer...

Convert a String to a Number
How can i convert a character to a number, so that the result is only numerical digits, and would always be the same, besides 26 (plus non-alpha characters) if-then statements. like, A = 1 and A always equals 1 so if I had the string:  1234A after converting, I would get the result:  12341 A doesn't necessarily have to = 1, or even a single digit.  It just has to equal a numerical... number. perhaps you can do something like this.  Store your letters in an array A and maintain a cooresponding array B that has the numeric equivalent you would want to repla...

automatic convert for float number
Hi, I'm using the string format {0:N} to display the value of a field. THe field has the datatype float and it is on a postgreqsql server. I use the databinding for it.Because of the different between SQL languages and windows cultural properties, we have to each time format the value to the right one, so the sql server can execute it.In postgresql, I have to write 1.5 not 1,5 for example.In German, we use 1,5 as the standard input format in windows.How can I automatically convert 1,5 to 1.5 without using Updating event ? Are there any property for that ?Thanks  There is a method ...

how to convert a number to string in SQL?
I search the help for T-sql, still don't know how to convert a number, for example 12345 to a string with formating like 12,345 Thanks for your help. The best place to do such formatting is in the front end, not at the database level.That being said, you could use a combination of the CONVERT and CAST functions (see CAST and CONVERT) to cast the number as a money type, then convert it to a varchar, and then strip off the decimal point and 2 0's at the end:DECLARE @test intSELECT @test = 12345SELECT REPLACE(CONVERT(varchar(50), CAST(@test AS money), 1),'.00','')Or, you could make use of a ...

Converting String to Float
Can anyone help me with the following please: I have the following class: using System; { public class Circle { float fxCenter, fyCenter, fRadius; // Constructor public Circle() { // Initialize internal variables fxCenter = 0; fyCenter = 0; fRadius = 0; } public float Top { get { return fxCenter - fRadius; } set { fxCenter = value + fRadius; } } public float Radius { get { return fRadius; } set { ...

How to convert float to varchar or string..
I've three columns: Length          Width            Height 1.5                  2.5              10 2                   3.7                19   in Query I want to display L...

How to convert from string to hex number
Hello, I have a requirement for passing a hex value like this to a function. Example code: my $ipmbAddress = getIpmbAddress(...); ## returns 82 getDeviceID($ip, $ipmbAddress); Here in the above code, the getDeviceID() fucntion will accept only 0x82 (hex number). How to convert the number 82 to 0x82 (Note that this should not be as string) I tried the following but not working properly. $ipmbAddress = "0x".$ipmbAddress; $ipmbAddress = hex("$ipmbAddress"); $ipmbAddress = sprintf(0x%0x, $ipmbAddress); getDeviceID($ip, $ipmbAddress); Please help. T...

Convert string to number #2
Hey all, I have got a text field in the summary band in a report datawindow which I want to set to a value at runtime and the value will come from the user. The value will be used for further addition and substraction. How do I convert this value from a string to a number? Thanks in advance for any help. Jian Kuang Depending on the scale or type of number, you can use Dec ( ), Long ( ), Double ( ), Real ( ), or Integer ( ). All of them call for a string or blob argument. Jian Kuang <jkuang@vitinc.com> wrote in message news:3757DC4F.41131683@vitinc.com... Hey a...

Web resources about - How do I convert strings to floating number? - perl.beginners

Floating production storage and offloading - Wikipedia, the free encyclopedia
A floating production, storage and offloading (FPSO) unit is a floating vessel used by the offshore oil and gas industry for the processing of ...

Facebook Increases Character Limit on Posts to 5000, Rolls Out Floating Navigation Bar and More Amid ...
... changes to Facebook’s interface. These include an expansion of the character limit on posts from 500 to 5,000, a rollout of the floating navigation ...

Sofia the First: The Floating Palace on the App Store on iTunes
Get Sofia the First: The Floating Palace on the App Store. See screenshots and ratings, and read customer reviews.

Cut Fifty Euro Note - Floating Away in Small Pieces - € - Flickr - Photo Sharing!
That's a close up of a torn, ripped, cut or shredded 50 Euro Note that is slowly chasing away into small pieces... _________________________ ...

Aussie surfers design ‘Seabin’ to collect floating waste
Two Australian surfers have developed an automated ocean rubbish bin which catches floating rubbish, oil, fuel and detergents &ndash; and it&rsquo;s ...

Fisherman finds man’s body floating facedown in Duck Creek, Silverwater - DailyTelegraph Search Search ...
THE dead man found floating in Duck Creek at Silverwater is a 27-year-old from Auburn, police sources have confirmed this morning.

Fisherman finds man’s body floating facedown in Duck Creek, Silverwater
THE dead man found floating in Duck Creek at Silverwater is a 27-year-old from Auburn, police sources have confirmed this morning.

Pacific Eden cruise ship rocked by claims it is a floating sick bay
... aboard P&amp;O's&nbsp;newly-launched Pacific Eden cruise liner, which docks in Sydney on Monday,&nbsp;allege&nbsp;it is a&nbsp;&quot;floating ...

Tattoos identify body of man found floating in Duck River at Clyde
The body of a man found floating in a river in Sydney's west has been identified after a friend recognised his tattoos.

It’s not magic, but this floating Bluetooth speaker is still awesome
... seen it a month earlier because it would have made the perfect gift for a friend or family member? Well, this is that thing. The ICEORB Floating ...

Resources last updated: 1/20/2016 7:34:19 PM