String To Format(#.##) to String using XojoCoreLocale

Hi,
I accept defeat and need a more resilient mind :smile: .

  1. I read a text file from a 3th program
  2. I put the values in my dictionary: note that these values use the dot format for decimal (example: 10.4042950849666)
  3. I need to convert it to a string but with the format (#. ####)

In short I need to do:
String 10.4042950849666 must become String 10.4042

IMPORTANT: I donā€™t know where my program will be used (America, Europe, Asia, ā€¦) so I have to deal with localization.

The best solution (I think) is to use Xojo.Core.Locale(ā€œen-USā€); this way I donā€™t care about anything, and no use of ReplaceAll().

Var locale As New Xojo.Core.Locale("en-US") '-> not used yet, and I have no idea how to use it in Format()
Var dicDataRefSim As New Dictionary
Var anArray() As String

anArray.AddRow("10.4042950849666") '-> here I read from 3th program the string and I add to my array
dicDataRefSim.Value("PLANE LONGITUDE") = Format(anArray(0).ToDouble, "#.####") '-> OUTPUT: 104042950849666,
'Oh my God, the dot is used as a separator..
Break

I am forced to use anArray(0).ToDouble because the documentation say that Format() wants the parameter as a double

OK OKā€¦ THE QUESTION:
How can I use Xojo.Core.Locale(ā€œen-USā€) in Format(anArray(0).ToDouble, ā€œ#.####ā€) ?

What version of xojo are you using?

The last: 2020 rel 1.2

In that case you could use the new:

Double.ToString(locale As Locale, format As String) As String

http://documentation.xojo.com/api/data_types/double.html#double-tostring

or you could search for the . and just cut 4 characters after that so you dont go through string>double>string conversion if you want to always work in strings and avoid any possible alteration of the data

or you could use regex to do the same as above

it really depends how much data youā€™re working with and if you need a speedy solution.

Hi

dicDataRefSim.Value("PLANE LONGITUDE") = anArrayTo4Dec(anArray(16)) 'note for : Inside anArray(16) there is value with dot notation (example 12.1234567)

And the funcion is:

anArrayTo4Dec(sStr As String) As String
Var locale As New Locale("en-US") 'Mi assicuro l'uso della decimal dot
Var value As Double

value = Double.FromString(sStr, locale) 'Converto in Double per necessitĆ  di Format()
sStr = Format(value, "#.####") 'Lascio solo i 4 decimali
sStr = sStr.ReplaceAll(",",".") 'Riconverto la VIRGOLA con il PUNTO
Return sStr

I donā€™t know if it can be useful for someone, so I post it.
Hi. :+1:

P.S.: Sorry for the Italian in the code notes

That looks a bit complicated to me.
If the original data is in international (en-US) format, you do not need to define a locale when reading the data.
On the output side you could delete both lines beginning with sStr = and just use
Return value.toString(locale.current, "0.0000")
or any other desired locale.

Hi Ulrich,
Yes, itā€™s true ! Right observation.

Var locale As New Locale("en-US") 'Mi assicuro l'uso della decimal dot
Var value As Double

value = Double.FromString(sStr) 'Converto in Double per necessitĆ  di Format()
Return value.toString(locale, "#.####") '-> I make sure dot is used _(because I don't know how to test it unless I want to mess with my computer: which I don't want!)

Now is bettter
Great Ulrich :+1:

1 Like