I'm new in Dart.
In the code bellow exception ccures :
void main(){
dynamic alpha = "String";
dynamic beta = 12;
print("Your code is so "+beta+" "+alpha);
}
Error :
type 'int' is not a subtype of type 'String'
Why when we use dynamic keyword to insist on telling the compiler for doing this job it's still got error? "combining string and other types"
When you declare your variable dynamic it does not mean the variable won't have a runtime type. It means the type can change:
dynamic alpha = 'String';
print(alpha.runtimeType); // prints String
alpha = 1;
print(alpha.runtimeType); // prints int
You can't do that with var. With var the compiler will infer the type, and it's fixed after that:
var beta = 'String';
print(beta.runtimeType);
beta = 1; // error: A value of type 'int' can't be assigned to a variable of type 'String'.
print(beta.runtimeType);
When you try to do "Your code is so " + beta you use the + operator of your String with an int paramter: beta.
You can see in the documentation that the + operator of String only accepts a String:
String operator +(String other);
If you wanted to use that operator you would have to convert the int variable to String:
print('Your code is so ' + beta.toString() + ' ' + alpha);
That's not remarkably beautiful. Instead of concatenation try string interpolation:
print('Your code is so $beta $alpha');
I am getting this error when unmarshalling an xml
java.lang.NumberFormatException:
at com.sun.xml.bind.DatatypeConverterImpl._parseDouble(DatatypeConverterImpl.java:232)
while checking on the xml and my classes for double values, I've found one of the elements of the xml that has a null value for its double attribute (I know it is double because the others of the same element have a double attribute)
<data num = "">...</data>
data class
public class Data implements Serializable {
...
#XmlAttribute(name = "num")
private double num;
//private Double num; //same problem as above
...}
can the num attribute be parsed to double if it was null? or is it fine?
I am trying to get a date from this text:
{InstantSeconds=1581504140},ISO,Europe/Paris resolved to 2020-02-12T11:42:20
I tried doing
def text = "{InstantSeconds=1581504140},ISO,Europe/Paris resolved to 2020-02-12T11:42:20"
text = text.replaceAll("[^\\d.]", "")
text = text.substring(10)
println "${text}"
int result= Integer.parseInt("${text}");
println result
But I'm getting
java.lang.NumberFormatException: For input string: "20200212114220"
I'm using this (for practice) https://groovyconsole.appspot.com/
Does anyone know why that happens?
The value is too long for an integer. Use a Long datatype:
Long result = text.toLong()
assert result.class.name == 'java.lang.Long'
assert result == 20200212114220
See the method below [Not Working as expected].I want double or int as method parameters and return as any data type [in my case return as int,double or string].
Requirement:
if the double or int value is zero then return an empty string else the real int or double value. I don't want to use dynamic as return type
private static T CheckZero<T>( dynamic val ) {
return Math.Sign(val) == 0 ? (T)Convert.ChangeType(string.Empty, typeof(string)) : val;
}
I'm trying to convert a string representing a double from invariant culture to a double in current culture representation, I'm concerned with how to get the new double representation to use the current number decimal separator of Current Culture.
I used the code below for the conversion :
public static double ConvertToDouble(this object inputVal, bool useCurrentCulture = false)
{
string currentSep = CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
string invariantSep = CultureInfo.InvariantCulture.NumberFormat.NumberDecimalSeparator;
if (inputVal.GetType() == typeof(string))
{
if (!currentSep.Equals(invariantSep))
{
inputVal = (inputVal as string).Replace(invariantSep, currentSep);
}
}
if (useCurrentCulture)
return Convert.ToDouble(inputVal, CultureInfo.CurrentCulture);
else
return Convert.ToDouble(inputVal);
}
But the above code always gives me a double with ".", although I use the CurrentCulture for example French supposed to give me a double with comma (",").
Many thanks in advance for any hint.
FreeDev
But the above code always gives me a double with "." as the NumberDecimalSeparator
No, it returns a double. A double is just a number. It doesn't have a NumberDecimalSeparator... only a culture does, and that's only applied when converting to or from strings. Talking about the separator for a double is like talking about whether an int is in decimal or hex - there's no such concept. 0x10 and 16 are the same value, represented by the same bits.
It's not really clear what you're trying to do, but it's crucial to understand the difference between what's present in a textual representation, and what's inherent to the data value itself. You should care about the separator when parsing or formatting - but after you've parsed to a double, that information is gone.
From the comments and your question i guess that you actually want to convert a string to a double with either InvariantCulture or current-culture. This double should then be converted to a string which is formatted by the current-culture datetime-format informations(like NumberDecimalSeparator).
So this method should do two things:
parse string to double
convert double to string
public static string ConvertToFormattedDouble(this string inputVal, IFormatProvider sourceFormatProvider = null, IFormatProvider targetFormatProvider = null)
{
if (sourceFormatProvider == null) sourceFormatProvider = NumberFormatInfo.InvariantInfo;
if (targetFormatProvider == null) targetFormatProvider = NumberFormatInfo.CurrentInfo;
if (sourceFormatProvider == targetFormatProvider)
return inputVal; // or exception?
double d;
bool isConvertable = double.TryParse(inputVal, NumberStyles.Any, sourceFormatProvider, out d);
if (isConvertable)
return d.ToString(targetFormatProvider);
else
return null; // or whatever
}
You can use it in this way:
string input = "1234.567";
string output = input.ConvertToFormattedDouble(); // "1234,567"
Note that i've extended string instead of object. Extensions for object are a bad idea in my opinion. You pollute intellisense with a method that you 'll almost never use (although it applies also to string).
Update:
If you really want to go down this road and use an extension for object that supports any kind of numbers as (boxed) objects or strings you could try this extension:
public static string ConvertToFormattedDouble(this object inputVal, IFormatProvider sourceFormatProvider = null, IFormatProvider targetFormatProvider = null)
{
if (sourceFormatProvider == null) sourceFormatProvider = NumberFormatInfo.InvariantInfo;
if (targetFormatProvider == null) targetFormatProvider = NumberFormatInfo.CurrentInfo;
if (inputVal is string)
{
double d;
bool isConvertable = double.TryParse((string)inputVal, NumberStyles.Any, sourceFormatProvider, out d);
if (isConvertable)
return d.ToString(targetFormatProvider);
else
return null;
}
else if (IsNumber(inputVal))
{
decimal d = Convert.ToDecimal(inputVal, sourceFormatProvider);
return Decimal.ToDouble(d).ToString(targetFormatProvider);
}
else
return null;
}
public static bool IsNumber(this object value)
{
return value is sbyte
|| value is byte
|| value is short
|| value is ushort
|| value is int
|| value is uint
|| value is long
|| value is ulong
|| value is float
|| value is double
|| value is decimal;
}
Usage:
object input = 1234.56745765677656578d;
string output = input.ConvertToFormattedDouble(); // "1234,56745765678"