In the following code, why does tempString2 equal ‘44.00000’ and not ‘44’?
Var tempString1 As String = "44"
Var tempString2 As String = tempString1.Val.ToString
In the following code, why does tempString2 equal ‘44.00000’ and not ‘44’?
Var tempString1 As String = "44"
Var tempString2 As String = tempString1.Val.ToString
Because the result of string.Val is a Double.
What does tempString1.ToInteger.ToString
give you?
You should define a format, .ToString(“0”) will give you “44”
.ToString(“0.0”) gives you “44.0”
.ToString(“0.00”) gives you “44.00” etc.
note you can also specify a locale but default is a long value with doubles.
Var tempString1 As String = "44"
Var tempString2 As String = tempString1.ToInteger.ToString
is probably what you want.
Yes, and this is summarised here:
https://documentation.xojo.com/api/data_types/double.html#double-tostring
Thank you @DerkJ , using .ToInteger rather than .Val has fixed it.
In this use I am dealing with phone number strings, so no need for Double.
What is the point of converting from string to number and back to string? Can’t you just use the original string as is?
As someone who has spent years with HR systems:
A phone number field should never be a ‘numeric’ field.
String, no formatting.
(The amount of time wasted, and pointless issues raised, by people putting that data into Excel and complaining about the loss of leading zeroes…)
ToString(“0000000000”) should fix that or use string…
I agree, and my phone numbers are TEXT fields.
As an explanation, it is the Work Phone Country Code popup field where I need to extract the ‘44’ from the rest of the text. I could use NthField(), but I chose to do a String>Number>String instead. I then use this ‘44’ in building a full phone number to use when they click the Phone button.
I would suggest using the RowTagAt to hold the country code, so you don’t have to go through the gyrations of parsing “44” out of the text.
I have a Method that returns an array of every country phone code which I add to the popup menu. To add the Row Tag, I would need to perform the same String>Number>String 200+ times, rather than just once, when I needed the phone code. RowTag, schmotag…
But thank you @Tim_Hare for the suggestion!
Fair enough. For anyone reading along who might have had the same question as me, what we’re doing here is taking advantage of the fact that val/ToInteger/etc., stops parsing at the first non-numeric character and returns a partial result instead of throwing an error. Thus “123abc” returns 123. It’s a clever use of an idiosyncrasy of the language. I’d be lying if I said I’ve never done something similar.
Whenever I do this I force a space in the String eg “123 abc” so the .Val is guaranteed to be a true-positive, in case the ‘abc’ name started with a number.
To the OP: This seems like depending on a ‘side-effect’ of a function rather than coding things explicitly… feels kind creepy, unreliable and definitely less readable.