KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Converting Reals to Strings for International SVG/XML use
PRODUCT: 4D | VERSION: 12 | PLATFORM: Mac & Win
Published On: August 5, 2010

As a vector graphics standard, SVG documents contain many numbers describing the location of certain elements in the image. When dynamically generating these positions based on existing data or changing sizes in 4D code it is very common to caclulate a Real value and then use the String command to change it to a Text value which is then appended to the SVG document.

This works perfectly in the US. In some international countries though, the Text value for a Real contains a comma "," instead of a period "." to mark the decimal position. The String command manages this difference and would return 3.14 in the US and 3,14 in Germany. In general this is a great feature, but when using SVG and XML this causes problems as these standards only accept the "." character to mark the decimal position.

For this reason it is generally a good idea to wrap the String command in your code when generating SVG. Here is a sample method which you can use:

   //Name: SVG_stringWrapper
   //Description: always returns Text with "." instead of "," in strings


C_TEXT($0)
C_REAL($1)

$0:=Replace string(String($1);",";".")


Below is a sample code snippet using the SVG SET ATTRIBUTE command and the SVG_stringWrapper method. It uses calculated Real values to describe the X and Y translation of an element and then creates Text that can be set as the transform attribute to assign them to the element:

$translate_x:=SVG_stringWrapper ($rectspace*$rectwidth)+($rectwidth/2)
$translate_y:=SVG_stringWrapper (0.65*$svg_height)
$text_transform:="translate("+$translate_x+","+$translate_y+")"
SVG SET ATTRIBUTE(*;"myPic";"myID";"transform";$text_transform)