Suppress commas in formatted numbers

jschuchert

Active Member
Licensed User
Longtime User
My application makes a lot of calculations, then stores them in a database. One of the lines looks like this:
1,3456.2347876653,1234.5678903614,descriptor
I don't have any control on the number of digits after the decimal during the calculation since they are being multiplied by functions of angles.

I use the following code to store the above line
filewrite(c,coord(0) & "," & coord(1) & "," & coord(2) & "," & coord(3))
The values are retrieved using strsplit(variable,",")

Circumstances now require that I reduce the number of digits after the decimal
Here is the code to do that:
filewrite(c,coord(0) & "," & format(coord(1),"N5") & "," & format(coord(2),"N5") & "," & coord(3)

When I do that, here is what the line of numbers looks like:
1,3,456.23478,1,234.56789,descriptor
Extra commas for the 'thousands' separator, negating my use of strsplit

Is there a way to suppress the commas and still be able to set the number of digits after the decimal?

I prefer NOT to change the separator when using strsplit.

Thanks, Jim Schuchert
 

mjcoon

Well-Known Member
Licensed User
I've wondered what is the difference in the Format() between:

· Fn - Fixed Point with n digits after the decimal point.
· Nn - Number format (dd,ddd,ddd.dddddd) with n digits after the decimal point.

Only "Number" format is shown with commas; perhaps "Fixed Point" doesn't have them?

Mike.
 

jschuchert

Active Member
Licensed User
Longtime User
Thanks for your quick reply, Mike, but I don't know what the syntax would be for the 'Fixed' point. The docs on format aren't too clear. My whole purpose is to minimize the number of digits appearing on the device to help the user do some editing there. Instead, I may try to do the editing with code but it is a pain with text files.
 

mjcoon

Well-Known Member
Licensed User
... I don't know what the syntax would be for the 'Fixed' point.

It's only a matter of replacing your "N5" in the Format() call with "F5".

But corwin42's idea of using the Round() function instead should also work. The Format() function should do rounding too, and thus generate the same answer, but it might just do truncation - who knows!

Mike.
 

jschuchert

Active Member
Licensed User
Longtime User
Mike and Markus,

That did it. Thanks so much. Again B4ppc does everything I need, so far. Incidentally, I looked at the 'round' function but it looked like it only rounded the number prior to the decimal. I also didn't pick up on the "Fixed" number thing until Mike explained it. You guys are awesome.

Jim Schuchert
 

mjcoon

Well-Known Member
Licensed User
Incidentally, I looked at the 'round' function but it looked like it only rounded the number prior to the decimal.

Hi again Jim, I didn't think that corwin42 would have got his advice wrong so I had a try myself.

B4X:
Round(87654321.2345678, 5)

yields "87654321.23457".

So it is correctly rounding the decimal part and rounding the "...678" up to ..."7".

Also, there are no commas at the millions or thousands.

But, like you, my preference would to do the explicit conversion from numeric to text with Format() rather than rely on Basic4PPC's implicit conversion (when, for instance, you present a numeric to a keyword that takes text parameters like MsgBox).

Regards, Mike.
 

jschuchert

Active Member
Licensed User
Longtime User
I didn't mean to imply by my previous message that corwin42 was incorrect. I meant that I had looked at the 'round' function during my search for a solution and just assumed that it did not round beyond the whole number. The example is inadequate, in my opinion. I applaud corwin42 for going beyond that and offer my apologies if I was misunderstood. I also tried the round function in my app and it works as described. Mike, you make a good point concerning using Format(). Thanks again to both of you. I have learned a lot.

Jim
 
Top