Issue
I am doing below operation on floating variables in cmake .
I read an earlier stack overflow cmake posts and modified my code for executing below expression. But I am not getting expected result:
Results:
### RESULT 30
### A 20.0
Expected results:
### RESULT 30.0
### A 20.0
cmake
set(A 20.0)
set(B 80.0)
set(loop 1 ) #0 1 2 3 4 5)
function(ARTH_OPER expr output)
execute_process(COMMAND awk "BEGIN {print ${expr}}" OUTPUT_VARIABLE __output)
set(${output} ${__output} PARENT_SCOPE)
endfunction()
foreach(loop_val ${loop})
ARTH_OPER("${A} + ${loop_val} * 10" RESULT)
message("### RESULT ${RESULT}")
message("### A ${A}")
endforeach(loop_val)
How can I get the expected result.
Solution
You could use awk
's printf
function and use the %f
format string for printing floating point numbers and .1
to print with one decimal. Combined it becomes %.1f
:
execute_process(COMMAND awk "BEGIN {printf(\"%.1f\", ${expr})}" OUTPUT_VARIABLE __output)
Answered By - Ted Lyngmo