6

I have below awk command which give mentioned result as well.

echo '1600000.00000000' '1600000.0000' | awk '{ print ($1 != $2) ? "true" : "false" }'

Result is : - false

As per numeric value the result given by the command is correct. But I want command must consider both input('1600000.00000000' '1600000.0000') as string and give the result as true. Because If you consider both input as string then they are not equal difference is there in precision.

2 Answers 2

10

From the GNU Awk user's guide, to force a number to be converted to a string, concatenate that number with the empty string ""

$ echo '1600000.00000000' '1600000.0000' |
  awk '{ print ($1"" != $2"") ? "true" : "false" }'
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you very much etopylight....Its worked for me. Thanks to stackoverflow team as well.
I think concatenating one of them is enough... ($1"" != $2)
@Sundeep That worked too :) didn't noticed that a string comparison can be forced if at least an operand is a string type
@AmolMurkute Cool, glad to help!
@etopylight : i usually like prepending the empty string instead ::::::::: :::::::::::: ::::::: :::::::::::: :::::: :::::: awk '{ print ($1 == _$2 ? "false" : "true") }' ::::::::::::::::: :::::::::::::: gapless string concat is actually a thing in awk
2

You can also use sprintf:

echo '1600000.00000000' '1600000.0000' |\
awk '{ print(sprintf("%f",$1) == sprintf("%f",$2)) ? "true": "false"}'
true

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.