# Stripping decimal places

#### Softselect

##### Member
Hi All,
in my app I need to strip decimal places from a string after calculatio
I need to strip to 3 places, but if the calculation only returns 2 then I get an error
Can some one please help, its late and I have brain freeze, cant seem to figure it out
Attached sample file
Thanks
Friedrich

#### Attachments

• teststring.sbp
897 bytes · Views: 141

#### Cableguy

##### Expert
Longtime User
I think you are not aproaching the problem the best way...

You are triming the string to find the decimal point and then fixing its length...

I would go with the StrSplit aproach, as it takes a string of any length and split it into any number of strings with a comom separator..

In this case, using "," as the separator we would get alway 2 strings(eg; 12,067 would result in a strig containing "12" and another containing "067")
Then by checking the length of the second (decimal) string you could perform whatever task associated...

Try this:
PS. although I think the code is correct, i cannot trim more than 2 number from the end of the string, can't figure why?

#### Attachments

• teste.sbp
1.2 KB · Views: 130
Last edited:

#### Scubaticus

##### Active Member
Hi All,
in my app I need to strip decimal places

Perhaps the Format command will help?

Format("+123456","F3") -> 123456.000
Format("+1.23456", "F3") -> 1.234

Scub

Last edited:

#### Rioven

##### Active Member
Longtime User
Rounding or stripping

Hi,

For just stripping decimal places you may also apply this solution: [@scubaticus: I've learned new one, thanks]
r="1E"&num1.Value
newnumber=Int(textbox1.Text*r)/r

For rounding decimal places:

r="1E"&num1.Value
newnumber=Int(textbox1.Text*r+.5)/r 'here you can manipulate the '.5' for rounding up or down.

or simply use..

newnumber=Round(textbox1.Text,num1.Value)

Regards,

Last edited:

#### Softselect

##### Member
Solved

Hi everyone,
I have solved my problem of restricting decimal places to 3
it is probebly not the best way but it works if you have less than 2 decimal places. (and i used a goto, big sin i know)
The reason i need to do it is, that I need to store it to a file for later printing of report via PC and this needs to be done via WIFI and automaticaly
on the PC i will use a utility that sits and look at a directory and print s any file that appears in it.

of corse this all still needs to be done!!!
just thought i might share it with yous
Danke
Friedrich

#### klaus

##### Expert
Longtime User
Hello,
I suggest you a solution in the joined file.
The idea is to calculate the number of digits after the decimal point.
Then
if the number is less then 3 you add the missing zeros
else you stripp it normaly.
I hope this helps you even you found anouther solution.

Klaus
Switzerland

#### Attachments

• teststring.sbp
1.3 KB · Views: 114

#### Softselect

##### Member
Hello,
I suggest you a solution in the joined file.
The idea is to calculate the number of digits after the decimal point.
Then
if the number is less then 3 you add the missing zeros
else you stripp it normaly.
I hope this helps you even you found anouther solution.

Klaus
Switzerland

Thank you Klaus,
That is what i was trying, but i am not actualy a programmer, so i tend to do tings the long way round.
:sign0188:
I will use it like u sugest.
Danke
Friedrich

#### klaus

##### Expert
Longtime User
Hello Friedrich
Do you want only to strip your data or do you want to round them before, in that case you can first round your numbers with the function
New = Round(Old,3)
and after that add the missing zeros if necessary as in the example I sent you.

Klaus Switzerland

#### Softselect

##### Member
Sample Solution

Hi Everyone,
Just in case someone is interested, I have written a new routing to trim decimal places, add missing zeros and add a decimal point if none is passed.
The routine is called with a variable for the number of decimal places.
Rounding is NOT needed in my case as I am working with measurements in microns. Rounding to 1 micron makes very little difference, because the basic accuracy of the instrument is 3 microns of a mm.
using the round() command also trims the string but does not add missing Zeros. However this can be used in my routine by un-remarking it.
I have included some remarks for clarity

Thanks to all for the input to make this work.
Greetings from
Friedrich, in Sunny South Africa

#### Attachments

• Decimal stripper 1v0.sbp
1.8 KB · Views: 120

#### Rioven

##### Active Member
Longtime User
Hi Softselect,

Nice string manipulations exercise!
Can also use StrIndexOf(org,".",0) to return value of point location.

Regards,

#### Softselect

##### Member
Hi Rioven,
Thank for this infos
I have updated my code with the use StrIndexOf(org,".",0) command
and i also used it to seach for "." presence.
Updated code attached

Thank you
Friedrich

#### klaus

##### Expert
Longtime User
Hello Friedrich

I agree with Rioven.

You can also replace the line