Regional independant Format() with dots-comma's

S

Sick

My application populates a ListBox from a .TXT file. In the textfile there
are prices with both dots as well as comma's for decimal indication.
Example:

1234990; xg-tr-45; 1700,50; 0
2662666; hj-54-56; 1565.00; 0
8228880; 30-56-tw; 3295.50; 0
0022339; hs-sa-73; 2975,75; 0
.... etc

The amounts (third column) are used to calculate a total. However, since I
have Dutch regional settings only the comma-seperations can be used for
calculating a correct sum. My system ignores the prices with the decimal
seperated by a dot (.) It is no option to alter the .TXT file.

I tried various options, such as formatting the entries with practically all
the options I could find. One example:

ItemAmount = Format(CSng(ItemAmount), "Currency")

Is there a simple solution for this, besides converting the prices? If
anyone could help, I would gratefully appreciate it.
 
T

Tom Shelton

My application populates a ListBox from a .TXT file. In the textfile there
are prices with both dots as well as comma's for decimal indication.
Example:

1234990; xg-tr-45; 1700,50; 0
2662666; hj-54-56; 1565.00; 0
8228880; 30-56-tw; 3295.50; 0
0022339; hs-sa-73; 2975,75; 0
... etc

The amounts (third column) are used to calculate a total. However, since I
have Dutch regional settings only the comma-seperations can be used for
calculating a correct sum. My system ignores the prices with the decimal
seperated by a dot (.) It is no option to alter the .TXT file.

I tried various options, such as formatting the entries with practically all
the options I could find. One example:

ItemAmount = Format(CSng(ItemAmount), "Currency")

Is there a simple solution for this, besides converting the prices? If
anyone could help, I would gratefully appreciate it.

Here is a little something, though in reverse :) It totals up the
values in an array of strings with mixed values.

Option Strict On
Option Explicit On

Imports System.Globalization

Module Module1

Sub Main()
Dim values() As String = {"1700,5", "1565.00", "3295.50", "2975,75"}
Dim total As Double

Dim nfi As NumberFormatInfo = New NumberFormatInfo
nfi.NumberDecimalSeparator = ","
nfi.NumberGroupSeparator = "."

For Each value As String In values
If value.IndexOf("."c) = -1 Then
total += Convert.ToDouble(value, nfi)
Else
total += Convert.ToDouble(value)
End If
Next

Console.WriteLine(total)
End Sub

End Module

Anyway, might give you a way to go :)
 
T

Tom Shelton

Here is a little something, though in reverse :) It totals up the
values in an array of strings with mixed values.

After thinking about it, thought I better add some comments :)
Option Strict On
Option Explicit On

Imports System.Globalization

Module Module1

Sub Main()
Dim values() As String = {"1700,5", "1565.00", "3295.50", "2975,75"}
Dim total As Double
' Create a NumberFomratInfo object to handle the values
' using the coma as the decimal separator. Need to specify
' both the group separator and the decimal separator or you'll
' get an exception... In your case, you'll want to reverse the
' the values.
Dim nfi As NumberFormatInfo = New NumberFormatInfo
nfi.NumberDecimalSeparator = "," ' you use . here
nfi.NumberGroupSeparator = "." ' you use , here

' Now we'll just iterate over the array and
' acumulate the total
For Each value As String In values

' If this value doesn't contain a . then
' it uses a comma and we need to use our
' custom numberformat object in the conversion
' otherwise, it's in good old US of A format :)
 
C

Cor

Hi Sick
I did found the solution from Tom nice. But Tom is not Dutch and this is a
normal problem in Holland. We use the US keyboards but there is a dot on the
numpad, while our decimal seperator is a comma. Overlooking it, I see you
are using a listbox where you are entering a textStrings. (For who reads
this beside Sick, there were Dutch keyboards, but they are rarely used).

The only thing I hope we are sure about is that the value field is the 3th
field after the ; So I think the solution can be rough typed not tested
\\\\
dim myStringValues() as String = Split(listboxItem,";")
dim myDoubleValue as double = Cdbl(replace(String(2),".",","))
///

You even can put a control in it to see if it is complete with
if myStringValue.count <> 4 then
' do error
Handling.

I myself would do it on another way and use this or another method to
populate a listview. Would look much nicer.

I hope this helps a little bit?

Cor
 
T

Tom Shelton

Hi Sick
I did found the solution from Tom nice. But Tom is not Dutch and this is a
normal problem in Holland. We use the US keyboards but there is a dot on the
numpad, while our decimal seperator is a comma. Overlooking it, I see you
are using a listbox where you are entering a textStrings. (For who reads
this beside Sick, there were Dutch keyboards, but they are rarely used).

Your right, I'm not Dutch :) But, I'm not sure why my solution won't
work... What am I missing?
 
C

Cor

Hi Tom,

I found it a very nice solution for doing an unstring with the same problem
and I did put it in my snippets. I did not say there is anything wrong with
it.

Then I looked it over again and saw the string was not made.

Therefore I did made an example. And while busy I thought, hell, normaly I
wou ld in this case only do replace the dot for a comma, so let me give that
example also.

And to show the world what problem we always have in Holland with that
numboard with the dot, I added the extra text.

But your example is nice espacialy when this is in a untstring, like I said
in the message to Sick and it stays in my snippets.
:)
Cor
 
T

Tom Shelton

Hi Tom,

I found it a very nice solution for doing an unstring with the same problem
and I did put it in my snippets. I did not say there is anything wrong with
it.

Then I looked it over again and saw the string was not made.

Therefore I did made an example. And while busy I thought, hell, normaly I
wou ld in this case only do replace the dot for a comma, so let me give that
example also.

And to show the world what problem we always have in Holland with that
numboard with the dot, I added the extra text.

But your example is nice espacialy when this is in a untstring, like I said
in the message to Sick and it stays in my snippets.
:)
Cor

Ok, I thought maybe it didn't work. I misunderstood. I am not a big
expert in i18n, so I thought maybe I was missing something. Very good
:) I have to ask, isn't it true that most of Europe uses the . and ,
opposite of the US? In other words - US = 1,050.75, Europe = 1.050,75?
Would that make this a common problem all over - or do most people use
special keyboards?

Just curious... I do have a book on the subject for VB6 (it is by
someone you may recognize - http://www.i18nwithvb.com/), but I never
really read it :) I started to, because of a project we were going to
take on - but then the project fell through. I wonder how much would
apply for .NET? I wonder if he will be writing a .NET version?
 
C

Cor

Hi Tom,

Europe is of course more and more one country, but we have a lot of regional
settings, languages and traditions, so I will try to explain as far as I
know it.

In the whole Europe with exception from Britain is used 999.999,99 (we think
the Brittains do it on another way because they do everything else than the
rest, but slowly they are using the more European things like the kilo, the
meter etc. and someday the Euro). I am not sure from Ireland (I think also
like Britain and is this one of the exceptions because normaly they are not
following them, we will hear that from Fergus when he reads this, he is
origonal Irish)

The dot is not always used, in case of your example you will mostly/often
see 1050,75. When it become larger amounts the dot can be used. I think
there is no rule for that.
:) I have to ask, isn't it true that most of Europe uses the . and ,
opposite of the US? In other words - US = 1,050.75, Europe = 1.050,75?

Most European Countries have their own keyboards with some exceptions.
The exception can be because some fools made the regional keyboards so
stupid that the where unusual (that was in Holland and in Poland I thought)
or just because they are never used.

In that case we use the US keyboard with special setting or not. In Holland
they do not, because than you get that rarely used Dutch keyboard. For the
Polish there are more settings, so they can do it because they have a lot of
extra characters in their language.

Most languages have extra characters when you compare it with the English
language. Dutch has that too but not like the German language which has
really one characters more. Dutch has only some "accents" to split words or
to set the tone.

A big wish of every developer in Holland and the Dutch speaking part of
Belgian and I think in all Europe where the US keyboard is used, is to have
the possibility to use the dot on the num pad as a comma, but till now I
never saw that.

Then there are also minimal 2 keyboards designs. Called QWERTY (that is
written on the left side on the 3rd row of the keyboard) and AZERTY.

You find QWERTY on the Latin languages speaking part of Europe. (French,
Italian, Spanisch and Portugese) and in the Slavian language speaking part.
I am only sure for French. There is too the Easern part where Cyrillic
characters are used (Like Creece, Russia, Bulgaria, and Ukraine). I know
nothing about their keyboards.

I am sure of that people who speak the German and French language uses
really there own keyboard, but from Italian and Spain I am not sure and the
same is for the Scandinavians, maybe because they have also a lot of extra
characters.

I think that the main reason why people are using the US keyboards in
Europe, is that they were at start seldom, while people were used already on
the IBM PC US. I know that Poland they switch almost totally from Azerty to
Qwerty. It is very difficult to type on a US keyboard when you are used to
your national language keyboard and visa versa.

I hope this is enough, maybe we get some comments from Fergus Irish and
Herfried German speaking in Austria, Armin from Germany and Jan, who is
definitely from a Scandinavian Country or all others who will learn us
something about this.

:)

Cor
 
Top