Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Wish to round float in integer

Status
Not open for further replies.

pawelf

MIS
Jan 3, 2010
5
DE
Hallo,

i wish to round any float in a integer in a (svg) path.
I try
awk '/[-+]?\d*\.?\d+/ {print int($1+0.5)}' input.svg>output.svg
but the output (in the file) is 0 0.
The regex works fine and the round is tested. What's the problem?
 
Just to be sure you use the right field you may try this:
print $1, int($1+0.5)

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
A svgpath look like this
.. d="m78.222,22.069c-1.4335,0.0073-2.8714,0.12168-3.7812, and so on
That's why i use the regex to find all (and only) the floats
 
With the posted sample, what is the expected result ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
The expected code is:
.. d="m78,22c-1,0-3,0-4,...
 
Try this:

Code:
awk '
        {
                line=[blue]$0[/blue]
                output=[red]"[/red][purple][/purple][red]"[/red]
                [gray]# for every float on the line[/gray]
                [olive]while[/olive] ([b]match[/b](line,[green]/[0-9]+[.]?[0-9]*/[/green])) {
                        [gray]# extract the float from the line[/gray]
                        number=[b]substr[/b](line,[blue]RSTART[/blue],[blue]RLENGTH[/blue])
                        [gray]# insert it into the output line[/gray]
                        output=output [b]substr[/b](line,1,[blue]RSTART[/blue]-1) [b]int[/b](number+0.5)
                        [gray]# save the rest of the line for further processing[/gray]
                        line=[b]substr[/b](line,[blue]RSTART[/blue]+[blue]RLENGTH[/blue])
                }
                output=output line
                [b]print[/b] output
        }
' input.svg > output.svg

Annihilannic.
 
It works fine from a file (f.e. awk.sh) but not from the command line.
Can i use it in this way?
awk -f float2integer.awk input.svg>output.svg
 
I wrote a little zenityscript:
Code:
#!/bin/bash
zenity --info --text "With this tool, you can convert the float number in a SVG file in a integer numbers. This will important reduce the size of the file.The convertetd file will save in the same directory like this script."
input=`zenity --title="Select a file" --file-selection --text="name of the file"`
awk '{
                line=$0
                output=""
                # for every float on the line
                while (match(line,/[0-9]+[.]?[0-9]*/)) {
                        # extract the float from the line
                        number=substr(line,RSTART,RLENGTH)
                        # insert it into the output line
                        output=output substr(line,1,RSTART-1) int(number+0.5)
                        # save the rest of the line for further processing
                        line=substr(line,RSTART+RLENGTH)
                }
                output=output line
                print output
        }
' "$input" > output.svg
zenity --info --text 'finished '
This works fine for me.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top