You could do something like:
memory=$( LC_ALL=C free -h | awk ' /^Mem/ { suffix = $2 sub(/[0-9.]*/, "", suffix) printf "%.0f%sB\n", $2, suffix }' ) (LC_ALL=C to make sure the numbers are printed using the . decimal radix (3.7G would be output as 3,7G in locales using comma as the decimal radix)).
On GNU/Linux systems, you can also do:
memory=$( awk '/^MemTotal/{print $2*1024}' < /proc/meminfo | numfmt --to=iec --format=%.0fformat=%0f --suffix=B ) Or:
memory=$( free -h | awk '/^Mem/{print $2}' | numfmt --from=iec --to=iec --format=%.0fformat=%0f --suffix=B ) (that one coping with locales where the decimal radix is not .).
Note that free on Linux reports that MemTotal field of /proc/meminfo. As per proc(5), that's the total usable RAM (i.e., physical RAM minus a few reserved bits and the kernel binary code). For the physical RAM, and for PCs, as pointed out by @StephenKit, you may be better off using dmidecode to get the information from the BIOS, though you'd need superuser privileges for that:
physical_memory=$( sudo dmidecode -t memory | awk ' awk $1'$1 == "Size:" {total +=&& $2 ~ /^[0-9]+$/ {print $2$3}' | numfmt --from=iec --suffix=B | awk '{total += $1}; END {print total * 1024}' | numfmt --to=iec --suffix=B --format=%.0fformat=%0f )