1

I'm just getting started with Talend and I would like to know how to divide a value from a CSV file and round it if possible?

Here's my job layout:

Job Layout

And here's how my tMap is configured:

tMap configuration

Chris
  • 5,442
  • 17
  • 30
user3449429
  • 124
  • 1
  • 3
  • 11

2 Answers2

1

Since var1 is defined as String you cannot apply the divide method. Try something like this for your output prix2 calculus:

(Float.parseFloat(row1.prix2)/2200f) + "Vr"

or something like that (I cannot read the text in the screenshot very well, actually)

Gabriele B
  • 2,665
  • 1
  • 25
  • 40
  • error: `Exception in component tMap_1 java.lang.NumberFormatException: For input string: "000009000,00" at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source) at java.lang.Float.parseFloat(Unknown Source) at mod_file_02.liban_0_1.liban.tFileInputDelimited_1Process(liban.java:1514) at mod_file_02.liban_0_1.liban.tFileList_1Process(liban.java:550) at mod_file_02.liban_0_1.liban.runJobInTOS(liban.java:2172) at mod_file_02.liban_0_1.liban.main(liban.java:2020)` – user3449429 Jul 25 '14 at 14:47
  • 1
    This is probably caused by the comma. See http://stackoverflow.com/questions/7571553/javascript-parse-float-is-ignoring-the-decimals-after-my-comma. – Marcus Rickert Jul 25 '14 at 15:11
1

I assume the "/r" is to add a new line? That won't actually work and will instead add a string literal "/r" to whatever other string you're adding it to. You also don't need to do that because Talend will automatically start a new line at the end of the row of data for your tFileOutputDelimited.

But more importantly, you're attempting to call the divide method on a string which obviously doesn't exist (how would it be defined?).

You need to first parse the string as a numeric type (such as float/double/Big Decimal) and then divide by another numeric type (your Var1 is defined as a string in your example, so will actually fail there too because a string must be contained in quotes).

So typically you would either define the schema column that you are dividing as a numeric type (as mentioned) or you'd attempt to parse the string into a float in the tMap/tJavaRow component.

If you have your prices defined as something like a double before your tMap/tJavaRow operation that divides then you can use:

row1.prix2 / Var.var1

Or to round it to two decimal places:

(double)Math.round((row1.prix2 / Var.var1) * 100) / 100

You can also use a tConvertType component to explicitly convert between types where available. Alternatively you could parse the string as a double using:

Double.parseDouble(row1.prix2)

And then proceed to use that as previously described.

In your case though (according to your comment on Gabriele's answer), there is a further issue in that Java (and most programming languages) expect numbers to be formatted with a . for the decimal point. You need to add a pre-processing step to be able to parse your string as a double.

As this question's answers show, there are a couple of options. You can use a regex processing step to change all of your commas in that field to periods or you can use a tJavaRow to set your locale to French as you parse the double like so:

NumberFormat format = NumberFormat.getInstance(Locale.FRENCH);
Number number = format.parse(input_row.prix2);
double d = number.doubleValue();

output_row.nom = input_row.nom;
output_row.code = input_row.code;
output_row.date = input_row.date;
output_row.ref = input_row.ref;
output_row.ean = input_row.ean;
output_row.quantitie = input_row.quantitie;
output_row.prix1 = input_row.prix1;
output_row.prix2 = d;

And make sure to import the relevant libraries in the Advanced Settings tab of the tJavaRow component:

import java.text.NumberFormat;
import java.util.Locale;

Your output schema for the tJavaRow should be the same as the input but with prix2 being a double rather than a string.

Community
  • 1
  • 1
ydaetskcoR
  • 53,225
  • 8
  • 158
  • 177