473,474 Members | 1,312 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Dealing with "." or "," in decimal string expression

Hello,

I would like to return the good single value from a string value
in these cases :

Convert.ToSingle("23,30");
Convert.ToSingle("23.30"); // Conversion Error !!!

The result should be a single 23,30 (Single Type)

How to deal with this "." or "," for the decimal point ?
How to bypass this error and make it work ?
Regards,
Cybertof.

Nov 15 '05 #1
3 2099
You probably have to change it by hand

int i = testString.IndexOf(".");
if(i != -1)
{
string[] splitString = testString.Split('.');
if(splitString.Length > 2)
// ERROR
if(splitString.Length < 2)
{
if(i = 0) // ".xx"
testString = "0," + splitString[0];
else // "xx."
testString = splitString[0];
}
else
testString = splitString[0] + "," + splitString[1];
}

--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 15 '05 #2


Cybertof wrote:
I would like to return the good single value from a string value
in these cases :

Convert.ToSingle("23,30");
Convert.ToSingle("23.30"); // Conversion Error !!!

The result should be a single 23,30 (Single Type)

How to deal with this "." or "," for the decimal point ?
How to bypass this error and make it work ?


Well, you have to be aware that different countries and different
language conventions interpret 23,30 differently, for instance in Germay
decimls are separated with a comma so the above is interpreted as the
floating point number with the whole number part being 23 and the
fractional part being 30. However in Great Britain or the USA the
decimals are separated with a dot so there 23.30 is the same what people
in Germany write as 23,30.
..NET provides the Culture class to encapsulate such conventions and you
can pass such a culture to the Convert.ToSingle, here is an example
program that reads in strings from the command line and shows the result
of converting them under different cultural conventions:

using System;
using System.Globalization;

public class Test20031110 {
public static void Main (string[] args) {
string[] cultureNames = {
"en-US",
"en-GB",
"de-DE",
"de-CH"
};
CultureInfo[] cultureInfos = new CultureInfo[cultureNames.Length];
for (int i = 0; i < cultureNames.Length; i++) {
cultureInfos[i] = CultureInfo.CreateSpecificCulture(cultureNames[i]);
}
for (int i = 0; i < args.Length; i++) {
Console.WriteLine("Converting string {0}; output culture is {1}",
args[i], CultureInfo.CurrentCulture);
for (int j = 0; j < cultureInfos.Length; j++) {
try {
Console.Write("Converting with culture {0}: ", cultureInfos[j]);
float s = Convert.ToSingle(args[i], cultureInfos[j]);
Console.Write("converted to {0}.", s);
}
catch (Exception e) {
Console.Write("error occured: " + e);
}
finally {
Console.WriteLine();
}
}
Console.WriteLine();
}
}
}

So you need to decide from which culture your input stems and then
convert it with the intended cultural conventions
--

Martin Honnen
http://JavaScript.FAQTs.com/

Nov 15 '05 #3
Thanks Morten & Martin.
Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jean | last post by:
Hello everyone, I was having the following problem with a query, and after failing to find a similar solution on these newsgroups I decided to post here. I am quite new to Access, so would...
32
by: Licheng Fang | last post by:
Basically, the problem is this: 'do' Python's NFA regexp engine trys only the first option, and happily rests on that. There's another example: 'oneself' The Python regular expression...
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
10
by: =?Utf-8?B?SmF5QWNoVGVl?= | last post by:
I have a web service written in C# Visual Studio 2005 that calculates splits between two or more timekeepers in a transaction. To set this up I have several decimal data type variables from one...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.