By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,230 Members | 2,436 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,230 IT Pros & Developers. It's quick & easy.

dumb question... is there a built-in method to remove trailing zeros in datatype decimal?

P: n/a
Bob
I'm about to convert to string and use regex, but I thought there must be
something I'm missing here that already exists.

Bob

Nov 20 '05 #1
Share this Question
Share on Google+
15 Replies


P: n/a
use the re expression in a Format

MyStr = Format(5459.4, "##,##0.00") ' Returns "5,459.40".
--
Regards - One Handed Man

Author : Fish .NET & Keep .NET
=========================================
This posting is provided "AS IS" with no warranties,
and confers no rights.
"Bob" <no*****@nospam.net> wrote in message
news:eV***************@tk2msftngp13.phx.gbl...
I'm about to convert to string and use regex, but I thought there must be
something I'm missing here that already exists.

Bob

Nov 20 '05 #2

P: n/a
Bob
Please explain to me how this can be applied as a general solution.

"One Handed Man" <te***************************@BTOpenworld.com> wrote in
message news:%2****************@tk2msftngp13.phx.gbl...
use the re expression in a Format

MyStr = Format(5459.4, "##,##0.00") ' Returns "5,459.40".
--
Regards - One Handed Man

Author : Fish .NET & Keep .NET
=========================================
This posting is provided "AS IS" with no warranties,
and confers no rights.


Nov 20 '05 #3

P: n/a
"Bob" <no*****@nospam.net> schrieb
I'm about to convert to string and use regex, but I thought there
must be something I'm missing here that already exists.


Could you please give us an example of which values have to be formatted
how?
--
Armin

Nov 20 '05 #4

P: n/a
Bob
Okay, I think I got it.

Function RemoveTrialingZeros(ByVal dec As Decimal) As Decimal
Return CDec(CSng(dec))
End Function

Bob

"Bob" <no*****@nospam.net> wrote in message
news:eV***************@tk2msftngp13.phx.gbl...
I'm about to convert to string and use regex, but I thought there must be
something I'm missing here that already exists.

Bob


Nov 20 '05 #5

P: n/a
Bob
324.0000 -- 324
324.0100 -- 324.01
324.0001 -- 324.0001

"Armin Zingler" <az*******@freenet.de> wrote in message
news:OX**************@TK2MSFTNGP09.phx.gbl...
"Bob" <no*****@nospam.net> schrieb
I'm about to convert to string and use regex, but I thought there
must be something I'm missing here that already exists.


Could you please give us an example of which values have to be formatted
how?
--
Armin


Nov 20 '05 #6

P: n/a
"Bob" <no*****@nospam.net> schrieb
324.0000 -- 324
324.0100 -- 324.01
324.0001 -- 324.0001


The type of the values in the left column is not Decimal, it is String.
There are no trailing zeros in Decimals. The default behavior is that there
are *no* trailing zeros when converting a Decimal to a String, that's why I
don't understand the question:

Dim d As Decimal

d = 324D
MsgBox(d.ToString) 'returns "324"

d = 324.01D
MsgBox(d.ToString) 'returns "324.01"

d = 324.0001D
MsgBox(d.ToString) 'returns "324.0001"
--
Armin

Nov 20 '05 #7

P: n/a
Hello,

"Bob" <no*****@nospam.net> schrieb:
324.0000 -- 324
324.0100 -- 324.01
324.0001 -- 324.0001


Is that contained in a string like "bla foo baz 342.0000 bar baz foo 234.02
bla"?

Regards,
Herfried K. Wagner
--
MVP VB Classic, VB.NET
http://www.mvps.org/dotnet
Nov 20 '05 #8

P: n/a
Bob
Okay, you got me. It's for when I'm displaying the decimal value as a string. I
have some data being returned that comes back as datatype decimal that always
trails with 4 zeros even if the value is integer. I just wanted to chop off the
unnecessary zeros and thought a ...

oh hell.

Here it is.

Dim foo As Decimal = CDec("1.0100")
MsgBox(Format(foo, "#.####"))

thanks for kicking my brain into gear.

Bob

"Armin Zingler" <az*******@freenet.de> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
"Bob" <no*****@nospam.net> schrieb
324.0000 -- 324
324.0100 -- 324.01
324.0001 -- 324.0001


The type of the values in the left column is not Decimal, it is String.
There are no trailing zeros in Decimals. The default behavior is that there
are *no* trailing zeros when converting a Decimal to a String, that's why I
don't understand the question:

Dim d As Decimal

d = 324D
MsgBox(d.ToString) 'returns "324"

d = 324.01D
MsgBox(d.ToString) 'returns "324.01"

d = 324.0001D
MsgBox(d.ToString) 'returns "324.0001"
--
Armin


Nov 20 '05 #9

P: n/a
Sorry i didnt get back sooner, bu you saw the light anyway.

Well done

--
Regards - One Handed Man

Author : Fish .NET & Keep .NET
=========================================
This posting is provided "AS IS" with no warranties,
and confers no rights.
"Bob" <no*****@nospam.net> wrote in message
news:e6**************@tk2msftngp13.phx.gbl...
Okay, you got me. It's for when I'm displaying the decimal value as a string. I have some data being returned that comes back as datatype decimal that always trails with 4 zeros even if the value is integer. I just wanted to chop off the unnecessary zeros and thought a ...

oh hell.

Here it is.

Dim foo As Decimal = CDec("1.0100")
MsgBox(Format(foo, "#.####"))

thanks for kicking my brain into gear.

Bob

"Armin Zingler" <az*******@freenet.de> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
"Bob" <no*****@nospam.net> schrieb
324.0000 -- 324
324.0100 -- 324.01
324.0001 -- 324.0001


The type of the values in the left column is not Decimal, it is String.
There are no trailing zeros in Decimals. The default behavior is that there are *no* trailing zeros when converting a Decimal to a String, that's why I don't understand the question:

Dim d As Decimal

d = 324D
MsgBox(d.ToString) 'returns "324"

d = 324.01D
MsgBox(d.ToString) 'returns "324.01"

d = 324.0001D
MsgBox(d.ToString) 'returns "324.0001"
--
Armin

Nov 20 '05 #10

P: n/a
"Bob" <no*****@nospam.net> schrieb
Dim foo As Decimal = CDec("1.0100")


Shorter:
Dim foo As Decimal = 1.01D

;-)
--
Armin

Nov 20 '05 #11

P: n/a
I was not aware of this suffix method. Now I am!!
Good stuff Armin, you keep telling it, I'll keep learning it

;-D

--
Regards - One Handed Man

Author : Fish .NET & Keep .NET
=========================================
This posting is provided "AS IS" with no warranties,
and confers no rights.
"Armin Zingler" <az*******@freenet.de> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
"Bob" <no*****@nospam.net> schrieb
Dim foo As Decimal = CDec("1.0100")


Shorter:
Dim foo As Decimal = 1.01D

;-)
--
Armin

Nov 20 '05 #12

P: n/a
Bob
Shorter and useless to the example! :P

"Armin Zingler" <az*******@freenet.de> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
"Bob" <no*****@nospam.net> schrieb
Dim foo As Decimal = CDec("1.0100")


Shorter:
Dim foo As Decimal = 1.01D

;-)
--
Armin


Nov 20 '05 #13

P: n/a
True, True, but never the less, its good to know.

--
Regards - One Handed Man

Author : Fish .NET & Keep .NET
=========================================
This posting is provided "AS IS" with no warranties,
and confers no rights.
"Bob" <no*****@nospam.net> wrote in message
news:OQ**************@TK2MSFTNGP10.phx.gbl...
Shorter and useless to the example! :P

"Armin Zingler" <az*******@freenet.de> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
"Bob" <no*****@nospam.net> schrieb
Dim foo As Decimal = CDec("1.0100")


Shorter:
Dim foo As Decimal = 1.01D

;-)
--
Armin

Nov 20 '05 #14

P: n/a
"One Handed Man" <te***************************@BTOpenworld.com>
schrieb
I was not aware of this suffix method. Now I am!!
Good stuff Armin, you keep telling it, I'll keep learning it

;-D


If you wanna learn more: ;-)

<F1>
Visual Studio.NET
Visual Basic and Visual C#
Reference
Visual Basic language
Visual Basic Language Tour
Features
Data Types
Data type declarations
-> Type characters

See also chapter 2.4 in the language specifications.

--
Armin

Nov 20 '05 #15

P: n/a
"Bob" <no*****@nospam.net> schrieb:
Shorter and useless to the example! :P


\\\
Dim foo1 As Decimal = CDec("1.0100")
Dim foo2 As Decimal = 1.01D
///

The code above will be compiled to:

\\\
.locals init ([0] valuetype [mscorlib]System.Decimal foo1,
[1] valuetype [mscorlib]System.Decimal foo2)
IL_0000: nop
IL_0001: ldstr "1.0100"
IL_0006: call valuetype [mscorlib]System.Decimal
[Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.DecimalType ::F
romString(string)
IL_000b: stloc.0
IL_000c: ldc.i4 0x65
IL_0011: ldc.i4 0x0
IL_0016: ldc.i4 0x0
IL_001b: ldc.i4 0x0
IL_0020: ldc.i4 0x2
IL_0025: newobj instance void [mscorlib]System.Decimal::.ctor(int32,
int32,
int32,
bool,

unsigned int8)
IL_002a: nop
IL_002b: stloc.1
IL_002c: nop
///

--
Herfried K. Wagner
MVP VB Classic, VB.NET
http://www.mvps.org/dotnet
Nov 20 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.