views:

129

answers:

1

I am working in a project using openframeworks and I've been having some problems lately when writing XMLs. I've traced down the problem to a sprintf:

It seems that under certain conditions an sprintf call may write commas instead of dots on float numbers (e.g. "2,56" instead of "2.56"). In my locale the floating numbers are represented with a ',' to separate the decimals from the units.

I am unable to reproduce this behaviour in a simple example, but I've solved the problem by stringifying the value using a stringstream.

I am curious about the circumstances of sprintf using a different localization. When sprintf uses ',' instead of '.' and how to control it?

+7  A: 

The decimal separator is controlled by the LC_NUMERIC locale variable. Set setlocale for details. Setting it to the "C" locale will give you a period. You can find out the characters and settings for the current locale by looking in the (read-only) struct returned by localeconv.

Rob Kennedy
As a side note, you should probably never use any of the locale categories except `LC_CTYPE`, `LC_COLLATE`, and `LC_MESSAGES` unless you really know what you're doing. The rest just tend to mess things up and offer little benefit.
R..