473,776 Members | 1,665 Online
Bytes | Software Development & Data Engineering Community
+ 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.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 ?
Regards,
Cybertof.

Nov 15 '05 #1
3 2124
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)
// 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.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 = {
"en-US",
"en-GB",
"de-DE",
"de-CH"
};
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
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
2875
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 expression are incompatible" when the query runs.
32
14706
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
1
6509
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 at /usr/local/lib/php/php.ini. Any ideas why the setting does not seem to be having an effect? ...
10
2984
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 transaction) I execute the following line of code:
0
9628
marktang
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9464
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10122
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10061
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9923
tracyyun
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8954
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5368
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5497
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4031
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 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.