IsNumeric() method works for only Integer value not for decimal value
like this
String str = '10';
system.debug('===strToDec=='+str.isNumeric());
This will return true.
String str = '10.25';
Decimal strToDec = decimal.valueOf(str);
system.debug('===strToDec=='+strToDec);
String decToStr = String.valueOf(strToDec);
system.debug('===decToStr=='+decToStr);
Run this code in developer console ad check output.
In you case do something like. First convert string to numeric then check and again convert string into decimal for future use
String str = '10.25';
Integer intCheck = Integer.ValueOf(str);
Decimal decVal = Decimal.ValueOf(str);
if(String.ValueOf(intCheck ).isNumeric())
//use decVal here
Don't understand why you need this.
Answer from Ratan Paul on Stack ExchangeIsNumeric() method works for only Integer value not for decimal value
like this
String str = '10';
system.debug('===strToDec=='+str.isNumeric());
This will return true.
String str = '10.25';
Decimal strToDec = decimal.valueOf(str);
system.debug('===strToDec=='+strToDec);
String decToStr = String.valueOf(strToDec);
system.debug('===decToStr=='+decToStr);
Run this code in developer console ad check output.
In you case do something like. First convert string to numeric then check and again convert string into decimal for future use
String str = '10.25';
Integer intCheck = Integer.ValueOf(str);
Decimal decVal = Decimal.ValueOf(str);
if(String.ValueOf(intCheck ).isNumeric())
//use decVal here
Don't understand why you need this.
As Nick Cook suggested, this is one of the rare cases when catching the exception is probably the cleanest approach:
Object o = ...
Decimal d;
try {
d = Decimal.valueOf(String.valueOf(o));
} catch (TypeException e) {
d = null;
// In a controller you might add the exception message e.g. "Invalid decimal: abc"
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, e.getMessage()));
}
Videos
The format method automatically does the commas and periods or visa versa (depending on whether your profile is in the US or elsewhere)
Assuming the $ sign applies to all cases in your org you can simply use
Decimal input = 2000;
String output = '$' + String.valueOf(input.format());
Note the String.valueOf(). This is required because the input variable is defined as a Decimal.
Edit: I noticed a bug with the Decimal.format() method where it will not show any numbers past the decimal point if there are only zeros there, ex. $100.00. To solve this I came up with this method.
private String getCents(Decimal x){
String y = String.valueOf(x);
String z = '.';
if(y.contains(',')) z = ',';
y = y.substring(0, y.indexOf(z));
if(x - Decimal.valueOf(y) == 0)
return String.valueOf(x.format()) + z + '00';
else return String.valueOf(x.format());
}
Then to update the example above, it would be:
Decimal input = 2000;
String output = '$' + getCents(input);
This should work, if its until 2 decimals:
Decimal dec;
String amount;
if (!string.valueof(dec.format()).right(3).contains('.')){
amount = '$' + string.valueof(dec.format()) + '.00';
}else if (string.valueof(dec.format()).right(2).contains('.')){
amount = '$' + string.valueof(dec.format()) + '0';
}else {
amount = '$' + string.valueof(dec.format());
}
The + is used to add two numbers together, and to concatenate String values. When you use a non-string next to a String, toString() will be called on the object. As such, you can simply say:
System.debug(a.get('Name')+' '+a.get('AnnualRevenue'));
You can't directly cast a non-String to a string, as the following is also an error:
String output = (String)1.23;
But you can use the + operator with a string on either side:
String output = ''+1.23;
String output = 1.23+'';
I often use this as a quick technique to convert most primitives to a String.
You can only use typecasting between compatible types, like a Decimal to an Integer (with precision loss) or a String to an Id.
You can avoid typecasting here. As it is not required here. Even if you want to typecast you could use String.valueOf(a.get('AnnualRevenue'))