Dave's Brain

Browse - programming tips - float to int in bash

Date: 2008jan4
Language: bash

Q. How do I convert a floating point number into an integer in bash?

A.  Use the powerful ${} construct:

	#!/bin/sh
	FLOAT=1234.567
	INT=${FLOAT/\.*}
	echo $INT

We replace dot and all that follows it with nothing.
Notice that this truncates (does not round).
What this info useful to you? You can donate to say thanks

Add a comment

Sign in to add a comment
Copyright © 2008-2010, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.