PDA

Click to See Complete Forum and Search --> : [RESOLVED] round not rounding in CRX


drag0n_45
May 19th, 2008, 03:09 PM
Hello all. I have this function to convert to scientific notation:

Function (StringVar dInput)
local NumberVar iCount := 0;
local NumberVar dCurrent := cdbl(dInput);
local StringVar sReturn := "";

//dcurrent := dinput
while (dCurrent<-10 or dCurrent>10) do
(
dcurrent := dcurrent /10 ;
iCount := iCount+1;
);

round(dCurrent, 1) & "E" & sgn(dInput) * int(iCount);

The function works great, only problem is that I get something like "3.4E3.00" instead of "3.4E3".

Any reason why that anyone can think of?

jggtz
May 19th, 2008, 10:28 PM
Try :

ToText(round(dCurrent, 1),2) & "E" & ToText(sgn(dInput) * int(iCount),0);

or

ToText(round(dCurrent, 1)) & "E" & ToText(sgn(dInput) * int(iCount),0);

rasinc
May 19th, 2008, 10:34 PM
Looks like your problem is sgn(dInput) * int(iCount); showing decimals, not the Round function. Not sure why but try to piece out that part and see what happens with just parts of the line before you build it up again.

drag0n_45
May 20th, 2008, 10:37 AM
but why when the string concatenate does the first part of the output have decimals then?

rasinc
May 20th, 2008, 09:52 PM
When you issued the command
round(dCurrent, 1)

You asked it to round to 1 decimal place. So you got it rounding to 3.4 according to your example.

drag0n_45
May 21st, 2008, 07:54 AM
Ah. I must have mistyped. for, say, 34000 with my routine I get 3.40E8.00. This obviously isn't scientific notation. I'm going to try the recommendations here and get back to you.

drag0n_45
May 21st, 2008, 07:58 AM
jggtz, your examples helped, but didn't completely solve the problem. Somehow, they fixed the "E2.00" part of the improper result, but now I still have 3.40E2" as a decimal. I'm like to only see this rounded to one decimal place.

As I was typing this I changed this:

ToText(round(dCurrent, 1),2) & "E" & ToText(sgn(cdbl(dInput)) * int(iCount),0);

to this:

ToText(round(dCurrent, 1),1) & "E" & ToText(sgn(cdbl(dInput)) * int(iCount),0);

and it fixed the problem. Thanks a lot.

jggtz
May 22nd, 2008, 12:13 PM
I think that you shouldn't restrict the decimals in the left side of the number
5 723 400 000 ----> 5.7234E9 Is correct
5 723 400 000 ----> 5.7E9 Is NOT correct

drag0n_45
May 22nd, 2008, 12:38 PM
I agree, however I'm implementing scientific notation for the sake of space. In my other elements of my program (a log for instance) the scientific notation values are all rounded to the nearest tenth, so I'm sticking with it for consistency.