Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Working with Decimal 1

Status
Not open for further replies.

beautieee

Programmer
Oct 4, 2008
46
MY
Im working with decimal calculation and wish to seek for advise on getting desire results.

Q1. How to round the number when the number is more than 1?
(normally, number rounded when its greater than 5).

eg: 0.0033 will round to 0.004, using normal round fucntion of round(A,3) will returned 0.003.

Q2. How to calculate number of decimal places?
eg: 0.00008 will returned 5 decimal place.

Appreciate reply in advance.
Regards,
Beautieee
 
A litle amendment on Q1.

Rounded of decimal carry 2 digits, whenever the 3rd digits appeared, the second digit will roundup 1 number.

eg: 0.00341 will round to 0.0035.
0.0000286 will returned 0.000029

Thank you.
 

Q 2, you can do this:
Code:
Dim decNo As Double

decNo = 0.00008
[green]'Find position of period[/green]
Debug.Print InStr(1, CStr(decNo), ".")
[green]'Get value after decimal[/green]
Debug.Print Mid(CStr(decNo), InStr(1, CStr(decNo), "."))
[green]'Bingo[/green]
Debug.Print Len(Mid(CStr(decNo), InStr(1, CStr(decNo), "."))) - 1

Have fun.

---- Andy
 
If you can determine how many decimal places there are, then just do this:
Code:
Function MyRound(TheNumber As Single, NoDecPlaces As Integer) As Single
    MyRound = Format$(TheNumber + (5 * 10 ^ (-NoDecPlaces)), "0." & String(NoDecPlaces - 1, "0"))
End Function

?MyRound(0.00341,5)
 
Thank you very much to SBerthold and Andrzejek. Very glad to receive your reply. Will try and feedback to you later.

Thank you and Regards,
Beautieee.
 
Thank you Andy, its very helpful.

Regards,
Beautieee
 
SBerthold ,

Your solution given is very near to my requirement, anyhow i need something specific to round-up automatically without specifying how many digit of decimal point, but return only 2 significant value.

eg:-

0.0002340 will returned 0.00024
0.0023001 will returned 0.0024
0.0000123 will returned 0.000013
1.0000300 will returned 1.1

Any better idea?

Thank you.
Beautieee
 
well, pass the results of Andrzejek's solution (the part which returns the number of decimal places) to my function...I thought this would be obvious...sorry.

You will need to tweak my function some if passing whole numbers without decimals, and the argument "NoDecPlaces" is not the number of decimals to round to, but the number of decimals present (Therefore it was named "MyRound")
 

I don't think you will ever have 1.0000300 returned 1.1

You would have to sepcify its own specific rule of rounding just this one.....

Have fun.

---- Andy
 
Thank you very much to both of you. I had it done with a very traditional way to check :-

If Text1.Text < 0.001 And Text1.Text > 0.0001 Then
check 3rd digit if exist
then A to plus text1 0.00001 follow by left(text1,7)
or B text1 remained unchanged.

Just to get better solution if any.

Regards,
Beautieee
 
>You would have to sepcify its own specific rule of rounding

The OP has indeed specified their own rules...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top