2
$\begingroup$

I have real numbers in array, I want to round (or truncate) these numbers to 2-digit after decimal point, then I want to write values to Excel File For example if I have

t = {75.34123, 80.567,  85.789};

I want to have after rounding

t = {75.34, 80.56, 85.78};

then I want to export it to excel file. First I use N function but it shows number as I want and leave it in background as it was. so when I export it it writes the number as it before rounding. I found Solution that use "Computer Arithmetic" package and ComputerNumber Function that give me what I want, but when I export to excel it writes number as formula not as value. so I need solution that gives real Rounding so the number becomes as 2-digit after decimal point in all calculations, and writes these numbers as values to excel file. Thank you.

$\endgroup$
3
  • $\begingroup$ You mean truncating not rounding - as rounding would turn 85.789 into 85.79. You could look at using NumberForm[t, 4] $\endgroup$
    – flinty
    Oct 6, 2021 at 11:39
  • $\begingroup$ NumberForm Shows number as I want but in memory it stays as it, so if I use in some calculation it use 85.789 instead of 85.79, so it is not what I want. $\endgroup$
    – user81348
    Oct 6, 2021 at 11:58
  • $\begingroup$ Do you want exact decimal rounding or a 64-bit binary floating-point approximation? $\endgroup$
    – Michael E2
    Oct 6, 2021 at 12:08

3 Answers 3

8
$\begingroup$

You can use Round and specify the second argument,

t = {75.34123, 80.567, 85.789};
Round[t, 0.01]
(* {75.34, 80.57, 85.79} *)

to truncate, you can use the IntegerPart trick in the other answer.

$\endgroup$
1
  • $\begingroup$ thank you. I found Round Function but I did not know how to use the second parameter $\endgroup$
    – user81348
    Oct 6, 2021 at 15:16
5
$\begingroup$
t = {75.34123, 80.567, 85.789};
t2 = IntegerPart[100 t]/100 // N
Export["C:\\t2.xls", t2]
$\endgroup$
4
$\begingroup$

Truncating (toward zero), if including negative reals:

t = {75.34123, 80.567, 85.789, -13.579};
Sign[t] Floor[RealAbs[t], 0.01]
(*  {75.34, 80.56, 85.78, -13.57}  *)

If negative numbers not a concern, or truncating toward negative infinity:

Floor[t, 0.01]
(*  {75.34, 80.56, 85.78, -13.58}  *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.