c# - Converting a string with commas to double and int are different. Why? -


i migrating old mfc gui c#.

i building form-based gui when i've got unexpected exception converting string integer type. assumed work same converting string double.

string str = "1,000"; double dthou = convert.todouble(str);   // ok int ithou = convert.toint32(str);       // raises exception 

conversion double gives correct value: 1000.0. int conversion, able solution : convert.toint32() string commas.

but curious if there reason behind this. or, missing something?

i able find similar, not duplicate question : number parsing weirdness

[edit] after learning culture issue.

i in kind of culture-shock because until now, in korea, both floating point number , integer numbers expressed "," thousands group , "." decimal point (at least in real world, in korea, mean, think... ). guess have accept current settings of ms visual studio , carry on.

[edit2] after sleeping on issue.

i think it's more of inconsistent handling of formatted string. todouble accepts strings thousands separator (in culture, comma), toint32 not. if todouble float | allowthousands, why could'nt toint32 have been integer | allowthousands asking.

for double conversion, there 2 possibilities:

  1. in culture, , number group separator. , conversion succeeds , returns value of 1000.
  2. alternatively, in culture, , used decimal separator. again conversion floating point succeeds time returns 1.

for conversion integer, "1,000" not integer. suspicion, given naming, , number group separator you. , expecting treated way toint32(). toint32() not accept number group separators. valid characters toint32() 0 9, optional sign prefix of - or + , leading or trailing whitespace.


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -