LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   non numeric number to numeric (https://www.linuxquestions.org/questions/programming-9/non-numeric-number-to-numeric-4175545619/)

DoME69 06-17-2015 08:15 AM

non numeric number to numeric
 
how can i change non numeric number to numeric?

for Example:
22.00 to 22

Keith Hedger 06-17-2015 08:21 AM

I think you mean decimal to integer and it depends on the programming language you are using, please show what you have done and a bit more info.

Just seen your post tally, come on! After 157posts you should know better.

DoME69 06-17-2015 08:48 AM

Sorry.

bash :)

Keith Hedger 06-17-2015 08:58 AM

well BASH doesn't use decimals so I assume you are just using a string so just remove the decimal part like so:
Code:

keithhedger@LFSCerebro:~-> val=22.00
keithhedger@LFSCerebro:~-> echo ${val%.?*}
22


danielbmartin 06-17-2015 09:06 AM

Quote:

Originally Posted by DoME69 (Post 5378632)
how can i change non numeric number to numeric?

for Example:
22.00 to 22

Do you want to
(1) round to the nearest integer, or
(2) truncate (throw away the fractional part)?

Daniel B. Martin

DoME69 06-17-2015 09:11 AM

Thanks

Keith Hedger 06-17-2015 10:00 AM

To round to nearest integer use
Code:

keithhedger@LFSCerebro:~-> echo "scale=0;(22.00+0.5)/1"|bc -l
22
keithhedger@LFSCerebro:~-> echo "scale=0;(22.5+0.5)/1"|bc -l
23


chrism01 06-19-2015 05:32 AM

Agree with danielbmartin; that was my first thought/qn too ..

sundialsvcs 06-19-2015 07:13 AM

Also: why th'hell use bash?! :doh:

The scripting-language built into bash is most-basic ... and really never intended to be used to do anything more than stringing a few commands together and poking at its command-line.

If you want to do "real" things, use a "real" scripting language: Perl, Ruby, PHP, Python ... the list goes on and on. All of them probably already-installed on your machine. Any of which you can access by placing #!shebang line at the top of your script-file.

The output of the command, when run, will not reveal what language-system is actually being used, and you will have the full power of that language ... doing the sort of things that that language was intended to do ... at your disposal.

Frankly, even if you 'somehow monkey-wrench' Bash into producing an acceptable result, you'll be creating or perpetuating "an un-maintainable sack of ... :eek:." And there's plenty of that sort of thing on Planet Earth already! :) (And, BTW, I am intending that comment to have a purely technical, not personal, interpretation. "Given that you have the right tools for this job ready-at-hand, use the right tools for this job.")


All times are GMT -5. The time now is 05:35 PM.