Home
  • Products
  • Solutions
  • Support
  • Company
  • Products
  • Solutions
  • Support
  • Company
Community Forums Custom IC SKILL Test if string is Number

Stats

  • Replies 10
  • Subscribers 138
  • Views 397
  • Members are here 0

Test if string is Number

FranzD
FranzD 12 days ago

Hi,

I want to check if a string is a number or not. For example if I have  string1 = "10u" I want to use it to callculated some stuff but if string1 = "MET1" I just want to write it somewhere. All tips are appreciated

Thanks,

Franz

  • Reply
  • Cancel
  • Cancel
  • p94todorov
    p94todorov 12 days ago

    Hi Franz,

    you can try with numberp(cdfParseFloatString(string1)).

    Regards,

    Petar

    • Cancel
    • Up 0 Down
    • Reply
    • Cancel
  • FranzD
    FranzD 12 days ago in reply to p94todorov

    Thank you very much. That was exactly the function I was looking for.

    Regards,

    Franz

    • Cancel
    • Up 0 Down
    • Reply
    • Cancel
  • AaronSymko
    AaronSymko 12 days ago in reply to FranzD

    Another option is to use:

    numberp(aelNumber(string1))

    However, in certain cases (e.g. pcell code), using cdfParseFloatString() may be preferred.

    Regards,

    -Aaron

    • Cancel
    • Up 0 Down
    • Reply
    • Cancel
  • FranzD
    FranzD 10 days ago in reply to AaronSymko

    I tried aelNumber() before to test if its a float, but I always get Warnings when its not. The function mentioned before fits good for me. But I now discovered another problem: I not only need to check if its float or string but also if the string is an integer. Any suggestions for this? My main goal is to create CDF parameter variation of different cells. For this I read an check the parameters type and then decide how to variy them.

    Thanks for you replies

    Franz

    • Cancel
    • Up 0 Down
    • Reply
    • Cancel
  • Andrew Beckett
    Andrew Beckett 10 days ago in reply to FranzD

    Franz,

    I did originally think that maybe using atoi() would be the best approach, but since atoi("5u") returns 5, maybe that's not the best option. You might need to just do: pcreMatchp("^\\d+$" val)&&atoi(val)

    Andrew

    • Cancel
    • Up 0 Down
    • Reply
    • Cancel
  • FranzD
    FranzD 10 days ago in reply to Andrew Beckett

    Ok I used it and it seems to do what I need. I now check first with numberp(cdfParseFloatString(val) if val is a number, then check if val is integer or not with  pcreMatchp("^\\d+$" val)&&atoi(val). Thank you all for your help!

    • Cancel
    • Up 0 Down
    • Reply
    • Cancel
  • Andrew Beckett
    Andrew Beckett 10 days ago in reply to FranzD

    Hi Franz,

    By the way, you only really need to use the &&atoi(val) part if you want to return it as an integer; if you're just checking, then the pcreMatchp() would be sufficient.

    Regards,

    Andrew

    • Cancel
    • Up 0 Down
    • Reply
    • Cancel
  • FranzD
    FranzD 9 days ago in reply to Andrew Beckett

    Hi Andrew, 

    I also thought that since atoi(string) returns nil when string is not an integer, that this would be sufficient. I wanna test if its an integer and if so also do some calculations with it. My calcultaions are depending on the number type of the string. So I use different values for a float then for the iteger number. Since I check with numberp(cdfParseFloatString(string)) if string is a number it is then sufficient to use atoi(string) to check if string is an integer. If not so it must be a float. Is this right or do I miss something here?

    • Cancel
    • Up 0 Down
    • Reply
    • Cancel
  • FranzD
    FranzD 9 days ago in reply to FranzD

    Ok its not working only with atoi(). I need the pcreMatchp()&&atoi() also. Then it does everthing I want.

    • Cancel
    • Up 0 Down
    • Reply
    • Cancel
  • Andrew Beckett
    Andrew Beckett 9 days ago in reply to FranzD
    FranzD said:
    Ok its not working only with atoi(). I need the pcreMatchp()&&atoi() also. Then it does everthing I want.

    The problem is that numberp(cdfParseFloatString(string)) and atoi(string) would both return true (at least, non-nil) for both "5u" and "5". So it doesn't help to distinguish between the two. If atoi() had returned nil if there were any non-digit characters in the string, it would have been fine, but it doesn't, so that isn't a solution... hence the pcreMatchp suggestion.

    Andrew

    • Cancel
    • Up 0 Down
    • Reply
    • Cancel

Community Guidelines

The Cadence Design Communities support Cadence users and technologists interacting to exchange ideas, news, technical information, and best practices to solve problems and get the most from Cadence technology. The community is open to everyone, and to provide the most value, we require participants to follow our Community Guidelines that facilitate a quality exchange of ideas and information. By accessing, contributing, using or downloading any materials from the site, you agree to be bound by the full Community Guidelines.

© 2023 Cadence Design Systems, Inc. All Rights Reserved.

  • Terms of Use
  • Privacy
  • Cookie Policy
  • US Trademarks
  • Do Not Sell or Share My Personal Information