Ralf Schüßler
unread,Nov 13, 2012, 7:52:44 PM11/13/12You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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