Forum > C# > BUG: float.Parse and regional settings

BUG: float.Parse and regional settings

By camelot Posted 2012-04-08 11:43 Comments 9
camelot
  • Posts: 37

Some regional setting use comma as decimal separator. This lead to exception in float.Parse and other number parsing from strings.
Example: in Russian regional settings numbers used in way like that “20,2525”

My solution code

public static float FloatParse(string value)
{
  return float.Parse(value.Replace(",", "."), CultureInfo.InvariantCulture);
}

Here exception details

System.FormatException was unhandled
  Message=FormatException
  StackTrace:
       at System.Single.Parse(String s, NumberStyles style, IFormatProvider provider)
       at System.Single.Parse(String s)
       at cocos2d.CCParticleSystem.initWithDictionary(Dictionary`2 dictionary)
       at cocos2d.CCParticleSystem.initWithFile(String plistFile)

code:

m_tStartColor.r = float.Parse(ChangeToZeroIfNull(valueForKey("startColorRed", dictionary)));
camelot
  • Posts: 37

#1 RE: 2012-04-08 11:47

same bug with
int.Parse(“5.0252”);

Logan
  • Posts: 88

#2 RE: 2012-04-09 12:38

hi,
it seems that the int.Parse requires the string to be in the exact format.
i.e.,

int.Parse("5");

will work but

int.Parse("5.0252");

will throw a format exception

in your case you should use

 double.Parse("5.0252");
camelot
  • Posts: 37

#3 RE: 2012-04-10 20:56

i just reporting bugs, i dont need solutions. already commited fix in local git repository.

int.Parse(“5.0252”, NumberStyles.AllowDecimalPoint);
works perfectly.

int.Parse(“5.0252”, NumberStyles.AllowDecimalPoint);
throw errors in Russian regional settings (and any other regions where comma used as separator)

huangrh
  • Posts: 171

#4 RE: 2012-04-11 04:06

hi , Evgeniy Rojkov,
i think it’s better to use decimal point to seperate a float number, because comma sometimes is used to seperate interger, for example, “100,000”.
if you do want to use comma, you could add that function to your own native code.

camelot
  • Posts: 37

#5 RE: 2012-04-12 19:30

i dont care about comma or dot thing.
i just saying that on system with global settings with decimal delimeter as comma?
code like that:

int.Parse("5.0252");

throw FormatException.

cocos2d parse particles settings from *.plist and use alot of float.parse and int.parse in CCParticleSystem.cs

you can view this error, change regional settings to russian in emulator and run cocos default “tests” -> “particles”.

camelot
  • Posts: 37

#6 RE: 2012-04-17 13:54

Here my fix for CCParticle.initWithDictionary
please merge or refactor and merge this fix in to cocos2d-xna.

Parse error keep throwing in Russian WPes.

camelot
  • Posts: 37

#7 RE: 2012-04-17 14:01

Here my fix for CCParticle.initWithDictionary

CCParticleSystem.cs

http://snipt.org/uhggj8

HelperParse.cs

http://snipt.org/uhgha7

please merge or refactor & merge this fix in to cocos.
Parse error keep throwing in Russian WPes.

camelot
  • Posts: 37

#8 RE: 2012-04-17 14:20

in new master from 17.04.2012 12:25:35

System.FormatException was unhandled
  Message=FormatException
  StackTrace:
       at System.Single.Parse(String s, NumberStyles style, IFormatProvider provider)
       at System.Single.Parse(String s)
       at cocos2d.CCSpriteFrameCache.addSpriteFramesWithDictionary(Dictionary`2 pobDictionary, CCTexture2D pobTexture)
       at cocos2d.CCSpriteFrameCache.addSpriteFramesWithFile(String pszPlist)
       at tests.SpriteFrameTest..ctor()
       at tests.SpriteTestScene.createSpriteTestLayer(Int32 nIndex)
       at tests.SpriteTestScene.nextSpriteTestAction()
       at tests.SpriteTestDemo.nextCallback(CCObject pSender)
       at cocos2d.CCMenuItem.activate()
       at cocos2d.CCMenu.ccTouchEnded(CCTouch touch, CCEvent ccevent)
       at cocos2d.CCTouchDispatcher.touches(List`1 pTouches, CCEvent pEvent, Int32 uIndex)
       at cocos2d.CCTouchDispatcher.touchesEnded(List`1 touches, CCEvent pEvent)
       at cocos2d.CCApplication.ProcessTouch()
       at cocos2d.CCApplication.Update(GameTime gameTime)
       at Microsoft.Xna.Framework.Game.Update(GameTime gameTime)
       at tests.Game1.Update(GameTime gameTime)
       at Microsoft.Xna.Framework.Game.Tick()
       at Microsoft.Xna.Framework.Game.HostIdle(Object sender, EventArgs e)
       at Microsoft.Xna.Framework.GameHost.OnIdle()
       at Microsoft.Xna.Framework.MobileGameHost.RunOneFrame()
       at Microsoft.Xna.Framework.MobileGameHost.gameLoopTimer_Tick(Object sender, EventArgs e)
       at Microsoft.Xna.Framework.DispatcherTimerWin32.TimerElapsed(IntPtr hWnd, UInt32 uMsg, IntPtr nIDEvent, UInt32 uTime)

i know my english is bad.
here google search may help https://www.google.com/search?q=c%23+float.Parse+comma+dot+bug

camelot
  • Posts: 37

#9 RE: 2012-05-22 12:49

Loggin to reply

Copyright © 2010 - 2013 Cocos2d-x.orgClustrmaps