ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Trim numbers to x decimal places (https://www.excelbanter.com/excel-programming/292646-trim-numbers-x-decimal-places.html)

Andrea[_8_]

Trim numbers to x decimal places
 
Help please !!!!

I am trying to write some code that will trim general
numbers.

e.g. 123.123456789 to 123.1234

I have looked at Trim Statements but these only seem to
apply to characters and spacing.

Thanks Andrea

Frank Kabel

Trim numbers to x decimal places
 
Hi
try
=TRUNC(123.1234567,4)

--
Regards
Frank Kabel
Frankfurt, Germany

Andrea wrote:
Help please !!!!

I am trying to write some code that will trim general
numbers.

e.g. 123.123456789 to 123.1234

I have looked at Trim Statements but these only seem to
apply to characters and spacing.

Thanks Andrea


Tushar Mehta

Trim numbers to x decimal places
 
Hi Andrea,

Check the ROUND or TRUNC function.

--
Regards,

Tushar Mehta
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions

In article ,
says...
Help please !!!!

I am trying to write some code that will trim general
numbers.

e.g. 123.123456789 to 123.1234

I have looked at Trim Statements but these only seem to
apply to characters and spacing.

Thanks Andrea


No Name

Trim numbers to x decimal places
 
Hi Frank/Tushar

Thank you for both replies -

I've tried your suggestions, but I think an 'If' or 'case
statement' is required. I'm a VB beginner as you may have
worked out.

Something like:

Dim result As Variant
Dim number As Variant

number = Rst![Total]

Result = number

If number is .0000 then

Trim to .0000 '(4 decimal places)

frmMain.txtResult.Text = (Result)

However I do not know how to write this in VB and have
spent hours searching for an answer.

I hope I have made this a little clearer.

I would appreciate any help.

Thanks Andrea

-----Original Message-----
Hi
try
=TRUNC(123.1234567,4)

--
Regards
Frank Kabel
Frankfurt, Germany

Andrea wrote:
Help please !!!!

I am trying to write some code that will trim general
numbers.

e.g. 123.123456789 to 123.1234

I have looked at Trim Statements but these only seem to
apply to characters and spacing.

Thanks Andrea

.


Tom Ogilvy

Trim numbers to x decimal places
 
Dim result As Variant
Dim number As Variant

number = Rst![Total]

Result = number

If len(Number) len(Format(Number,"#.0000") then

Result = cdbl(format(Number,#.0000"))
End if

frmMain.txtResult.Text = (Result)

--
Regards,
Tom Ogilvy


wrote in message
...
Hi Frank/Tushar

Thank you for both replies -

I've tried your suggestions, but I think an 'If' or 'case
statement' is required. I'm a VB beginner as you may have
worked out.

Something like:

Dim result As Variant
Dim number As Variant

number = Rst![Total]

Result = number

If number is .0000 then

Trim to .0000 '(4 decimal places)

frmMain.txtResult.Text = (Result)

However I do not know how to write this in VB and have
spent hours searching for an answer.

I hope I have made this a little clearer.

I would appreciate any help.

Thanks Andrea

-----Original Message-----
Hi
try
=TRUNC(123.1234567,4)

--
Regards
Frank Kabel
Frankfurt, Germany

Andrea wrote:
Help please !!!!

I am trying to write some code that will trim general
numbers.

e.g. 123.123456789 to 123.1234

I have looked at Trim Statements but these only seem to
apply to characters and spacing.

Thanks Andrea

.




No Name

Trim numbers to x decimal places
 
TOM

Great !!! this works a treat.

Thank you...Thank you... Thank you...

Andrea

-----Original Message-----
Dim result As Variant
Dim number As Variant

number = Rst![Total]

Result = number

If len(Number) len(Format(Number,"#.0000") then

Result = cdbl(format(Number,#.0000"))
End if

frmMain.txtResult.Text = (Result)

--
Regards,
Tom Ogilvy


wrote in message
...
Hi Frank/Tushar

Thank you for both replies -

I've tried your suggestions, but I think an 'If' or

'case
statement' is required. I'm a VB beginner as you may

have
worked out.

Something like:

Dim result As Variant
Dim number As Variant

number = Rst![Total]

Result = number

If number is .0000 then

Trim to .0000 '(4 decimal places)

frmMain.txtResult.Text = (Result)

However I do not know how to write this in VB and have
spent hours searching for an answer.

I hope I have made this a little clearer.

I would appreciate any help.

Thanks Andrea

-----Original Message-----
Hi
try
=TRUNC(123.1234567,4)

--
Regards
Frank Kabel
Frankfurt, Germany

Andrea wrote:
Help please !!!!

I am trying to write some code that will trim general
numbers.

e.g. 123.123456789 to 123.1234

I have looked at Trim Statements but these only seem

to
apply to characters and spacing.

Thanks Andrea
.



.


Tushar Mehta

Trim numbers to x decimal places
 
I imagine Tom's code does the job, but I don't see the need for a test
as you want. Doesn't the following do the job?

frmMain.txtResult.Text = Round(Rst![Total],4)

--
Regards,

Tushar Mehta
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions

In article ,
says...
Hi Frank/Tushar

Thank you for both replies -

I've tried your suggestions, but I think an 'If' or 'case
statement' is required. I'm a VB beginner as you may have
worked out.

Something like:

Dim result As Variant
Dim number As Variant

number = Rst![Total]

Result = number

If number is .0000 then

Trim to .0000 '(4 decimal places)

frmMain.txtResult.Text = (Result)

However I do not know how to write this in VB and have
spent hours searching for an answer.

I hope I have made this a little clearer.

I would appreciate any help.

Thanks Andrea

-----Original Message-----
Hi
try
=TRUNC(123.1234567,4)

--
Regards
Frank Kabel
Frankfurt, Germany

Andrea wrote:
Help please !!!!

I am trying to write some code that will trim general
numbers.

e.g. 123.123456789 to 123.1234

I have looked at Trim Statements but these only seem to
apply to characters and spacing.

Thanks Andrea

.



No Name

Trim numbers to x decimal places
 
Hi Tushar

I have hundreds of text boxes to code. The data to
populate these ranges from 1 to 10 decimal places and I
need whole numbers to 0.

I am new to Vb so I'm sure your suggestion will come in
useful for future developments.

Thanks again Andrea

-----Original Message-----
I imagine Tom's code does the job, but I don't see the

need for a test
as you want. Doesn't the following do the job?

frmMain.txtResult.Text = Round(Rst![Total],4)

--
Regards,

Tushar Mehta
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions

In article ,
says...
Hi Frank/Tushar

Thank you for both replies -

I've tried your suggestions, but I think an 'If' or

'case
statement' is required. I'm a VB beginner as you may

have
worked out.

Something like:

Dim result As Variant
Dim number As Variant

number = Rst![Total]

Result = number

If number is .0000 then

Trim to .0000 '(4 decimal places)

frmMain.txtResult.Text = (Result)

However I do not know how to write this in VB and have
spent hours searching for an answer.

I hope I have made this a little clearer.

I would appreciate any help.

Thanks Andrea

-----Original Message-----
Hi
try
=TRUNC(123.1234567,4)

--
Regards
Frank Kabel
Frankfurt, Germany

Andrea wrote:
Help please !!!!

I am trying to write some code that will trim general
numbers.

e.g. 123.123456789 to 123.1234

I have looked at Trim Statements but these only seem

to
apply to characters and spacing.

Thanks Andrea
.


.



All times are GMT +1. The time now is 09:16 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com