cancel
Showing results for 
Search instead for 
Did you mean: 

Decimal zero disappearing when putting it in a Double

Former Member
0 Kudos

Hi,

i got a question. I dynamically create a table from field i get from a webservice. Some fields are currencies which i have to format. I loop through the field and create ddic:com.sap.dictionary.double attributes for those fields. I then parse the value as a String to a Double and apply formatting:


double doubleValue = Double.parseDouble(aValue);
DecimalFormat df = (DecimalFormat)numberFormat;
df.applyPattern("#0.00");
//format the value
result = df.format(doubleValue);

The problem is that when i create a Double of the value to put it into my node, the Double chops of any decimal zero's....so 237.10 will become 237.1 This make some of the data in my table have 2 decimals, but others 1. Does anyone know how i can prevent this?

Much thanks,

Hugo Hendriks

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

hi,

Try this,

// this is one way

1. Create a dictionary simple type of type double.

2. In Representation tab, give #.00 for Format.

3. Create context variable and assign its type to the above simple type.

4. Bind this variable to the UI element u want

5. Get the value as double from backend (let it be 123.4). Assign that value to the

context variable created.

The value displayed will be 123.40 which is the one u want.

The context variable now created accepts only double values(not string), so the value dispalyed will be right aligned

// this is nother way

In this case.. assuming it is coming with 2 decimal places from the backend.. create a value node.. inside the node which contains the output data. set its carinality to 1..1 .. create a value attribute of type big decimal in it.. or probably string.. if there is an issue in displaying BigDecimal directly..

map this new attribute to the necessary column in the table..

BigDecimal value = new BigDecimal("the actual value in string or long").setScale(2);

assign this to the value attribute created..

Thanks,

Lohi.

Former Member
0 Kudos

Hi Lohitha,

I tried this before but the only problem is that also other values which come from the backend which can have 1/2/4/...x decimals. This means i have to create simpletypes dynamically but i understood that this is impossible. Thats why i create attributes of the type Double, instead of fixed simpletypes...

any other solutions?

Thanx for the fast reply though,

regards,

Hugo

Former Member
0 Kudos

A little late, but I had the same problem and used this:

NumberFormat decimalFormatter = new DecimalFormat("0.00");

nodeValueAttribute.setNet_Price(new BigDecimal(decimalFormatter.format(lineItem.getUnitPrice())));

Note: lineItem.getUnitPrice() returns a double.

This works fine for me.

Regards,

Stu Higgs