Join decimal part

I have a list of mesures that each vector is like {0,1,-2,259} but it should be like {0.1,-2.259} So I did a rule, like list/. {a_,b_,c_,d_}->{a.b,c.d} but for the positives number it works fine! but for vector like this one I get {0.1,(-2).239} how can I fix this?

• No, is like i put it Sep 12 '14 at 20:55
• I exported the data from Matlab as csv, but my decimal symbol here is a , so... Sep 12 '14 at 20:59
• Sounds like you are after a kludge solution to a problem that ought to be fixed on the matlab side. Sep 12 '14 at 21:14
• Please note that the . in your rule is shorthand for Dot and not a decimal point! You almost certainly don't want the inner product of the whole part and fractional part! Sep 12 '14 at 22:06

May be I'm overthinking this one.

list = {0, 1, -2, 259};
f[x_, y_] :=  x + (2 Boole@NonNegative[x] - 1 ) y/10^IntegerLength[y]//N ;
list /. {a_, b_, c_, d_} -> {f[a, b], f[c, d]}

(* {0.1, -2.259} *)
• just beat me to it, yours is the same as mine (essentially):toDecimal[a_, b_] := N[a + (-1)^Sign[a] b 10^(-IntegerLength[b])]; list /. {a_, b_, c_, d_} :> {toDecimal[a, b], toDecimal[c, d]} (* {0.1, -2.259} *)
– chuy
Sep 12 '14 at 21:12
• @chuy Yup. (-1)^Sign[a] in definitively nicer. Sep 12 '14 at 21:15
• Thanks, this work, but can't understand how...it will take me more than an hour to understand it :) Sep 12 '14 at 22:27
• @Kafkarudo Take the code in the comment above by chuy. It does the same thing and I believe it's easier to understand Sep 12 '14 at 22:39

Similar to Alan's answer but a bit more direct:

fix = ToExpression @ ToString @ Row[#, "."] &;

in = {0, 1, -2, 259};

fix /@ Partition[in, 2]
{0.1, -2.259}
• Why not Row[#,"."]&/@Partition[in,2] ? Sep 13 '14 at 22:13
• @Kafkarudo I assumed that you wanted numeric values as output rather than merely expressions that look like numeric values. If not then yes, you may drop ToString and ToExpression. Sep 14 '14 at 0:19

The first output may look ok but it's not actually a number. See if this does what you want:

test = {0, 1, -2, 259}
combine[{a_, b_}] := ToExpression[ToString[StringForm[".", a, b]]]
Map[combine, ArrayReshape[test, {Length[test]/2, 2}]]
• similarly: ToExpression[StringJoin[Riffle[ ToString /@ #, "."]]] & /@ Partition[{0, 1, -2, 259}, 2] Sep 12 '14 at 21:19
ClearAll[f];
f = With[{left = FromDigits /@ IntegerDigits /@ #[[;; ;; 2]],
right = N /@ FromDigits /@ ({#, 0} & /@ IntegerDigits /@ #[[2 ;; ;; 2]]),
sign = Sign /@ #[[;; ;; 2]] /. 0 -> 1}, sign (left + right)] &;

list = {0, 1, -2, 259};
list2 = {0, 1, -2, 259, 3, 141592653};

Grid[{#, f@#} & /@ {list, list2} , Dividers -> All] f = With[{p = Length[IntegerDigits@#2]},
N[#1 + (2 UnitStep[#1] - 1) #2 10^(-p)]] &;
g = f @@@ Partition[#, 2] &;

Test case:

test = {{0, 1, -2, 259}, {-1, 2, 3, 124}, {-2, 34, -3, 45}, {1, 0, 3,
4}}
Grid[{#, "\[RightArrow]", g@#} & /@ test] Now strings would be required to deal with e.g.

test2 = {{"-1", "04", "0", "04"}, {"-1", "234", "2", "32"}, {"0",
"2345", "2", "003"}};

In this case:

h[u_] := N[
ToExpression[{#1, #2}].{1, (2 UnitStep[ToExpression@#1] -
1) 10^-StringLength[#2]}] & @@@ Partition[u, 2];

So,

Grid[{#, "\[RightArrow]", h@#} & /@ test2] list = {0, 1, -2, 259};
list /. {a_, b_, c_, d_} :> {a.b, If[Sign[c] == -1, -(-c).d, c.d]}
(*{0.1, -2.259}*)

ToExpression[
StringReplace[
ToString /@
Partition[{0, 1, -2, 259}, 2], {{"{" | "}" | (Whitespace )} :> "",
"," -> "."}]]

(*{0.1, -2.259}*)
• I am not the one who down-voted, but I see why: this does not work. Look at the FullForm -- you have Dot in the result, not a Real number. Sep 13 '14 at 3:05
• @Mr.Wizard I know. I have looked at it previously after some one down-voted. thanks. Sep 13 '14 at 3:16