1

Need to Converting Text field to currency in Apex:

String Amount = '989898.67'

was trying out something like :

    String s = ( Decimal.valueOf(i==null||i.trim()==”?’0′:i).setScale(2) + 0.001 ).format();
    String p = s.substring(0,s.length()-1);

Question - What all considerations that needs to mae about data integrity ? Any direct function available ?

1

1 Answer 1

2

Creating a formatted string version of a Decimal (currency) value is surprisingly difficult in Apex - see Apex - format number with more than 3 digits after decimal point.

But if you want the value as a number for calculations, then as you probably already know the Decimal class has all the methods you need to control the scale and rounding of the conversion:

Decimal d = Decimal.valueOf(!String.isEmpty(amount) ? amount : '0');
d = d.setScale(8, RoundingMode.HALF_EVEN);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .