Convert String to Double with formatting
-
Hi, I'm developing an WinForms application in VB.NET where the user can specify custom formats for numeric data entry fields. When I display data in lists, labels, editboxes, etc. I simply use Double.Format(strCustomFormat) to format the double values in the desired format - no problem here. But I'm having a problem converting the strings back to a double since I basicly don't know if the user is using a 1.234.567,09 or 1,234,567.09 format. So basicly I need a Double.Parse method where I can specify the format (similar to the ParseExact that DateTime has)... but this is not available as standard for Doubles :( Is there any other way I can do this ? Thanks you in advance. /Bean
-
Hi, I'm developing an WinForms application in VB.NET where the user can specify custom formats for numeric data entry fields. When I display data in lists, labels, editboxes, etc. I simply use Double.Format(strCustomFormat) to format the double values in the desired format - no problem here. But I'm having a problem converting the strings back to a double since I basicly don't know if the user is using a 1.234.567,09 or 1,234,567.09 format. So basicly I need a Double.Parse method where I can specify the format (similar to the ParseExact that DateTime has)... but this is not available as standard for Doubles :( Is there any other way I can do this ? Thanks you in advance. /Bean
-
Please read my thread again... it's far more complicated than that; a simple "," replace/removal won't do the job. For example... does "100,123" mean 100.123 or 100123.00 ... it's impossible to tell without actually knowing the format.
This is a complicated task especially since you are giving your user the freedom of customizing the formats. Like you have said: MrBean wrote: it's impossible to tell without actually knowing the format Therefore, I would suggest giving your user a list of choices. That way you can create your
Double.Parse
function according to the formats that you have allowed the user the choose from. I would suggest giving your user full freedom of specifying formats is a bad idea, especially when dealing with data. -
Hi, I'm developing an WinForms application in VB.NET where the user can specify custom formats for numeric data entry fields. When I display data in lists, labels, editboxes, etc. I simply use Double.Format(strCustomFormat) to format the double values in the desired format - no problem here. But I'm having a problem converting the strings back to a double since I basicly don't know if the user is using a 1.234.567,09 or 1,234,567.09 format. So basicly I need a Double.Parse method where I can specify the format (similar to the ParseExact that DateTime has)... but this is not available as standard for Doubles :( Is there any other way I can do this ? Thanks you in advance. /Bean
Hi there, I see the problem ur facing I had a similar one.You can do this little trick and I think the problem Should slove you basically have two formats here 1) 1.235.567,09 2) 1,234,567.09 To what I have understood,If format 1 is used there would be only one comma in the string and if format 2 is used there will be only single decimal So now you can parse the string and check how many decimals or commas exists in a string if decimals are more than one remove decimals and if commas are more than one remove commas. Now the question if what if there is only one comma and one decimal in the string. then Whatever comes first remove that and you should be good Hope this helps Let me know abt it Mandar Patankar Microsoft Certified professional
-
Hi, I'm developing an WinForms application in VB.NET where the user can specify custom formats for numeric data entry fields. When I display data in lists, labels, editboxes, etc. I simply use Double.Format(strCustomFormat) to format the double values in the desired format - no problem here. But I'm having a problem converting the strings back to a double since I basicly don't know if the user is using a 1.234.567,09 or 1,234,567.09 format. So basicly I need a Double.Parse method where I can specify the format (similar to the ParseExact that DateTime has)... but this is not available as standard for Doubles :( Is there any other way I can do this ? Thanks you in advance. /Bean
hye .. your problem looks very critical. well if you have the mask for the format which user is using then it is fine like ##,##,##.00 or ##.##.##,00 then you can see which option is decimal like in first . represents decimal and in next , represents decimal. but if this is not the case and you dont have the mask then tell me what is this 12,0 $ and 12.0 $ .. you have to tell me which one is 120 $ and which one is 12 $. now you cannot even visually tell which one is what and believe me I shall go opposite to your answer :-D .So restrict your user to one mask other wise you will end up with a big money theft in company account.
-
Hi there, I see the problem ur facing I had a similar one.You can do this little trick and I think the problem Should slove you basically have two formats here 1) 1.235.567,09 2) 1,234,567.09 To what I have understood,If format 1 is used there would be only one comma in the string and if format 2 is used there will be only single decimal So now you can parse the string and check how many decimals or commas exists in a string if decimals are more than one remove decimals and if commas are more than one remove commas. Now the question if what if there is only one comma and one decimal in the string. then Whatever comes first remove that and you should be good Hope this helps Let me know abt it Mandar Patankar Microsoft Certified professional
My current solution (which is not 100%) works a lot like you describes : detects which comes first, "," or "." But the case where there are no decimals but thousand delimiters... example : 123,456 It impossible to know if this means 123.456 or 12345.00 without knowing the input format. I was just wondering if there was a ".NET way" of converting strings back to doubles and specifying a format string which the parser should use... just like the ParseExact which the Date type supports just fine. It seems this is a big oversight from the .NET team. Unfortunately I can't limit the formats the users can enter to a predefined set - the users expect full freedom here and the old (non-.NET) version of the app had this functionality :(