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

  • Thread starter Thread starter Bob
  • Start date Start date
B

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
 
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 said:
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?
 
Okay, I think I got it.

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

Bob
 
Bob said:
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"
 
Hello,

Bob said:
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
 
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
 
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 said:
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
 
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.
 
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 said:
Shorter and useless to the example! :P
 
One Handed Man said:
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.
 
Bob said:
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
///
 
Back
Top