473,554 Members | 3,061 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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


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

Convert.ToSingl e("23,30");
Convert.ToSingl e("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 ?

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

int i = testString.Inde xOf(".");
if(i != -1)
string[] splitString = testString.Spli t('.');
if(splitString. Length > 2)
if(splitString. Length < 2)
if(i = 0) // ".xx"
testString = "0," + splitString[0];
else // "xx."
testString = splitString[0];
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.ToSingl e("23,30");
Convert.ToSingl e("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.ToSingl e, 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.Globaliz ation;

public class Test20031110 {
public static void Main (string[] args) {
string[] cultureNames = {
CultureInfo[] cultureInfos = new CultureInfo[cultureNames.Le ngth];
for (int i = 0; i < cultureNames.Le ngth; i++) {
cultureInfos[i] = CultureInfo.Cre ateSpecificCult ure(cultureName s[i]);
for (int i = 0; i < args.Length; i++) {
Console.WriteLi ne("Converting string {0}; output culture is {1}",
args[i], CultureInfo.Cur rentCulture);
for (int j = 0; j < cultureInfos.Le ngth; j++) {
try {
Console.Write(" Converting with culture {0}: ", cultureInfos[j]);
float s = Convert.ToSingl e(args[i], cultureInfos[j]);
Console.Write(" converted to {0}.", s);
catch (Exception e) {
Console.Write(" error occured: " + e);
finally {
Console.WriteLi ne();
Console.WriteLi ne();

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

Martin Honnen

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

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 appreciate any help/pointers in the right direction. The problem is that I keep getting the error to the likes of "Data types in the criterion...
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 engine doesn't exaust all the
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" setting to be "E_ALL", notices are still not getting reported. The perms on my file are 664, with owner root and group root. The php.ini file is located...
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 particular timekeeper: timerRow = 4.5M tkRate = 130M tkAmount = 585M (timerRow * tkRate) totalAmount = 1605M (sum of all tkAmounts in the...
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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 we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.