# C# Float text format in other countries

This topic is 1150 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have a problem that a tool I have written is outputting floats incorrectly for the game in various countries.

Specifically when I use a textwriter to output floats I typically get 1,000 when the game is expecting 1.000

At the moment they are having to do a global find and replace on ',' {comma} replacing it with '.' {dot}, but that's not really very good.

When I am reading the data I can use ..

float.Parse(nparts[0], System.Globalization.CultureInfo.InvariantCulture);


However when writing the data I cannot see the same interface anywhere.

Of course this problem has popped up when the tool is just about complete and contains close to half a million lines of code.

Anybody know a way of setting the culture for text output ?

Hopefully in a global way so I don't have to search the entire code base for ".Write"

##### Share on other sites

Maybe you can convert the floats to strings manually before writing them, with Single.ToString Method (IFormatProvider).

Instead of searching for every call to Write, you could just replace the actual Writer object. E.g. if you're using a TextWriter, make you own class CultureInvariantTextWriter which is a wrapper for an actual TextWriter, and then replace all your writers with that.

Actually, you just have to pass the appropriate IFormatProvider to your existing TextWriter's constructors.

When I am reading the data I can use ...Globalization.CultureInfo.InvariantCulture

I ran into that problem and same fix with reading floats too. I tried to give contract some art work out to an Italian, but my collada importer didn't work on his PC, as it expected ,'s instead of .'s

Edited by Hodgman

##### Share on other sites

Why do you not want to output values in a format that people expect to see in different countries? That is the whole point of the regional formatting in the first place, it means numbers and dates are rendered as people are used to seeing.

As long as you use the culture setting for the machine the read/write in the different formats will be as the current regional settings are configured.

Do your users a favour and show them values in the format they expect :)

##### Share on other sites

Why do you not want to output values in a format that people expect to see in different countries? That is the whole point of the regional formatting in the first place, it means numbers and dates are rendered as people are used to seeing.

It's an issue if these files are later parsed by your game.
e.g. A Canadian uses your tools to create a level.xml file. They share it to their friend in France, but it does not load correctly because it was written using one culture and read in another conflicting culture.

To solve this issue, you either need to tag all your bits of text with the appropriate culture to use when parsing them, or just say "fuck it, everyone can speak american!". For internal game data, that's not so horrible. Most places already write code in american...

##### Share on other sites

Why do you not want to output values in a format that people expect to see in different countries? That is the whole point of the regional formatting in the first place, it means numbers and dates are rendered as people are used to seeing.

As long as you use the culture setting for the machine the read/write in the different formats will be as the current regional settings are configured.

Do your users a favour and show them values in the format they expect

As someone who sits in a place where the user-expected output is different from the C locale: functions or whole programs which think they need to stick to some locale setting they detected somewhere are highly annoying and the cause of extremely frustrating problems (up to the point of utter uselessness because floating point numbers have ',' as the decimal separator and use ',' to separate numbers).
Sure, if you need to present a number to the home-and-garden end user, you probably want a localized output. But I have found that for non-trivial programs that is in the minority compared to what else regarding textual input/output you need and in some programming languages getting clean, non-regional text in- and output can be quite a drag.

##### Share on other sites

Do your users a favour and show them values in the format they expect

Sadly that won't help when they can't load the file in an application. The end user might look at a comma and know it is a decimal separator, but most applications don't.

For example the app can export in FBX format, they are telling me the binary FBX files are perfect and the text mode FBX files are barfed.

Actually, you just have to pass the appropriate IFormatProvider to your existing TextWriter's constructors.

That looks promising, but as usual I have ended up creating a whole load of work for myself.

I always use the paradigm ...

 using (TextWriter writer = File.CreateText(file))


Sigh.

Well nothing to do I guess but do a few find in files and some cut and paste coding.... yuck.

Cheers guys.

##### Share on other sites

but as usual I have ended up creating a whole load of work for myself.
I always use the paradigm ...

 using (TextWriter writer = File.CreateText(file))

Sigh.
Well nothing to do I guess but do a few find in files and some cut and paste coding.... yuck.

You can make an extension method for File, called CreateCultureInvariateText (etc), and then do a find/replace in files across your code

##### Share on other sites

You can set the default culture for a thread:

Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;


##### Share on other sites

Why do you not want to output values in a format that people expect to see in different countries? That is the whole point of the regional formatting in the first place, it means numbers and dates are rendered as people are used to seeing.

As long as you use the culture setting for the machine the read/write in the different formats will be as the current regional settings are configured.

Do your users a favour and show them values in the format they expect

As a Swede, I absolutely hate this. In 95% of all cases, localized applications are horrible to use, especially when it comes to floats.

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 9
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633717
• Total Posts
3013513
×