Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

round functionality awk

70 views
Skip to first unread message

Ralf Schüßler

unread,
Nov 13, 2012, 7:52:44 PM11/13/12
to
Hello,

the functionality of round a (before) unknown number without the limitations while using "sprintf()" with the %f I've needed and therefore I've read some sites of awk-books and google-results. I've not found suitable results. then I wrote my own solution. The solution works, but for myself it seems to be strange to switch between strings and numbers just to have this solution.

Anyone who have better or other ideas (bear in mind that I have numbers from 10^-9 ... 10^9 to round!)?

my solution is as follows:


function round( ROHWERT , STELLEN ) {
ROHWERT_b = sprintf("%."STELLEN"e" , ROHWERT ) ;
printf("ROHWERT_b : %."STELLEN + 3"e \n" , ROHWERT_b ) ;
split( ROHWERT_b , WERT_s , "e") ;
print "WERT[1] : " WERT_s[1] ;
print "WERT[2] : " WERT_s[2] ;
form = - WERT_s[2] + STELLEN ;
print "form " form ;
WERT_rnd = sprintf("%."form"f" , ROHWERT_b ) ;
print "WERT_rnd: " WERT_rnd ;
printf( "WERT_rnd: %s\n" , WERT_rnd ) ;
printf( "WERT_rnd: %f\n" , WERT_rnd ) ;
WERT_t = WERT_rnd ;
if ( match( WERT_t , "\\." ) != 0 ) {
sub("\\." , "," , WERT_t ) ;
} else {
sub("," , "." , WERT_t ) ;
}
printf("WERT_t: %s\n" , WERT_t ) ;
#
}
BEGIN {
if ( ROHWERT == "" || STELLEN == "" ) {
print "Aufruf: " ;
printf("awk -f scriptname -v ROHWERT=12345.999 -v STELLEN=4\n" ) ;
print "\"ROHWERT\" und \"STELLEN\" anpassen..." ;
} else {
a = round( ROHWERT , STELLEN ) ;
print a ;
}
}


Of course the print(f) statements are only for easy trying this example and of course sorry for my english and the german var-names in script.

Many thanks in advance,

Ralf Schüßler

Janis Papanagnou

unread,
Nov 14, 2012, 3:14:09 AM11/14/12
to
On 14.11.2012 01:52, Ralf Sch��ler wrote:
> Hello,
>
> the functionality of round a (before) unknown number without the limitations while using "sprintf()" with the %f I've needed and therefore I've read some sites of awk-books and google-results. I've not found suitable results. then I wrote my own solution. The solution works, but for myself it seems to be strange to switch between strings and numbers just to have this solution.
>
> Anyone who have better or other ideas (bear in mind that I have numbers from 10^-9 ... 10^9 to round!)?

In Arnold's awk manual you find a rounding function[*] that is not
switching between strings and number. So an option may be to first
scale your ROHWERT value, do the rounding, finally scale back again.
(That should also work in the range you have given.) Just a thought.

Janis

[*] http://www.gnu.org/software/gawk/manual/gawk.html#Round-Function
> Ralf Sch��ler
>

r...@infad.de

unread,
Mar 20, 2013, 7:21:26 PM3/20/13
to
Am Mittwoch, 14. November 2012 09:14:09 UTC+1 schrieb Janis Papanagnou:
>
> In Arnold's awk manual you find a rounding function[*] that is not
>
> switching between strings and number. So an option may be to first
>
> scale your ROHWERT value, do the rounding, finally scale back again.
>
> (That should also work in the range you have given.) Just a thought.
>
>
>
> Janis
>
>
>
> [*] http://www.gnu.org/software/gawk/manual/gawk.html#Round-Function
>

Hello,

and first: thanks. Second: sorry, I'ven't really written what I wanted - sometimes some found this just after posing the question. The most important thing for me was the scaling functionality, the round functionality I found not problematic.
The scaling seems for me to be possible with using the sprintf %e function, but that's all (for me).

Third: I have not everything in the example understood. I wanted to try it again an I was hoping to be able to pose a new question more concrete, but I wasn't. I'm sorry.

Thanks again and best regards,

Ralf Schuessler
0 new messages