I am using bash
I have a script that takes a number, i.e. 85.152, which is always a non integer and essentially tries to get that number to be a multiple of 10. My code is as follows:
Explanation:
1. I convert from a float to integer first because I cannot simply divide 85.152 by 10 using bash. It does not like dividing floats.
2. I subtract 20 because this is desired by design (it can be ignored). The division by 10 gets me to a number below 10 which is just a trick because any integer less than 10 which is multiplied by 10 will simply be a multiple of 10.
3. I convert from float to integer again just in case the number I divided, (to get less than 10), ends up dividing out to be another float (i.e. 85/10=8.5, then truncate to 8).
4. Finally I multiply by 10, 8*10=80 which is a multiple of 10.
I did what I could as a newbie to make it work but now I would like to simplify this process. I simply want to take 85.152 and make it a multiple of 10 that is 20 less than 85.152. So answer = (85.125-20) truncated to a multiple of 10. Should be 60. Any suggestions?
Hi RudiC...
Neat!
Contracting on your method, OSX 10.7.5, default bash terminal:-
EDIT:-
Important note both methods do not work with the number 80 but do if converted to 80.0 float equivalent.
Last edited by wisecracker; 10-24-2015 at 01:22 PM..
Reason: See above...
I need to divide the number of white spaces by total number of characters in a file using bash. I am able to get the number of white spaces correctly using:
tr -cd < afile | wc -c
I am also able to get the total number of characters using:
wc -c afile
How do I divide the first... (2 Replies)
I found the following issue by simply increasing a variable. The ((A++)) expression returns an error, the other expressions both return 0. Does anyone know why?
script.sh:
#! /bin/bash
A=0
B=0
C=0
((A++)) ; echo "${?}"
((B=B+1)) ; echo "${?}"
((C+=1))... (8 Replies)
I have 2 numbers
xmin = 0.369000018
xmax = 0.569000006
and want to calculate
(xmax- xmin) / 5.0
I have tried using $(( )) but is always giving an error (8 Replies)
:confused:We have two servers one active and one stand by
as follows Active 202.61.9.9
Stand by 202.61.9.10
Float IP 202.61.9.8
What is use of this float IP?
How it is configured? (1 Reply)
Hi people :)
I'm learning shell scripting using bash and I want to generate 4 floating point number with 5 decimal places and write them to a file and a variable. I've done all this except the $RAMDOM enviroment variable does not generate a float number but a integrer.
I hope you could... (3 Replies)
I have a file with 2 columns . One of the line looks like the following.
Is it possible to convert every float no in column 2 to integer.
input
NM_032881 6.03787973608527e-05
output
0.0000603787..... (2 Replies)
Friends,
I have a file with contents like:
interface Serial0/4/0/0/1/1/1/1:0
encapsulation mfr
multilink
group 101
Now I need to manipulate the file in such a way that to all the numbers less than 163, 63 gets added and to all numbers greater than 163, 63 gets deducted.(The numbers... (2 Replies)
Good morning,
I'm testing the use of ceilf:
/*Filename: str.c*/
#include <stdio.h>
#include <math.h>
int main (void)
{
float ceilf(float x);
int dev=3, result=0;
float tmp = 3.444f;
printf("Result: %f\n",ceilf(tmp));
return 0;
} (1 Reply)