This is my code:
float = 10.23444566
awk {printf("%.0f\n", $float)}
I am getting the answer as 0.
I just want to get the value 10 out of it.
i am using awk command .
This is my code:
float = 10.23444566
awk {printf("%.0f\n", $float)}
I am getting the answer as 0.
I just want to get the value 10 out of it.
i am using awk command .
Try this.
float=10.23444566;
printf "%.0f" $float;
In shell scripting, the syntax you used for printf
command is wrong. You cannot call it like a c
function.
See man pages for details.
man printf
If you want to use the awk
command, you need to pipe the float
value to the command. See the code below.
float=10.23444566;
echo $float | awk '{printf "%.0f\n", $1}';
I believe this is the answer the OP is looking for:
> float=10.23444556; echo $float | awk '{ printf "%d\n",$1 }'
10
>
This is similar:
> float=10.2344456; awk -v x="${float}" 'BEGIN{ printf "%d\n", x }'
10
>
Well, the (Posix-)Shell does not have floating points, and when we talk about floats, it makes sense to talk about mantissa and exponent, and it is unclear what you mean by "integer" part.
Also, your statement to assign to the variable float
is syntactically wrong, so you would get already an error message at this point.
If you have a string, which might not look like a float, but at least like a fractional number (as it is in your example), and you want to extract the part to the left of the fractional point, this goes like this:
float=10.45671
int_part=${float%.*}
You need to think about the cases, where you have a negative number, or when you really have something which looks like a general float, i.e. 12.157E+03
.