473,411 Members | 1,889 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

Real Simple Question

Hello,

I have a "smallmoney" field in SQL Server that I am
attempting to display, but when I use the .ToString()
method on a decimal variable it displays too many places
after the decimal point (i.e. more than two). I know
there's a string I could use as an argument for ToString
() that will only display the first two deigits after the
decimal point, does anyone know what that is? Thanks for
your help!

decimal AccountTotal = 4.7300;

string myString = AccountTotal.ToString("????");
Nov 18 '05 #1
3 1089
The .#### is normal for money types in SQL Server. For output in an ASP.NET
app, you can use DataBinder.Eval to output. The number will still have 4,
but only show 2.

<%# DataBinder.Eval(Container.DataItem, "Price", "{0:c}") %>

This involves late binding, and may not work for all situations. Now, if you
wish to include the $ or other country formatting, you can do this trick:

CurrencyLabel.Text = doubleVal.ToString("C",
Thread.CurrentThread.CurrentCulture);

If it is always dollars, you can use:

CurrencyLabel.Text = doubleVal.ToString("C", new CultureInfo("en-us"));

NOTE that I am using a double here. If you use
System.Data.SqlTypes.SqlMoney, you will ALWAYS have a precision of 4.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************** ********************
Think Outside the Box!
************************************************** ********************
"Mark Fox" <in**@solelsoftware.com> wrote in message
news:10****************************@phx.gbl...
Hello,

I have a "smallmoney" field in SQL Server that I am
attempting to display, but when I use the .ToString()
method on a decimal variable it displays too many places
after the decimal point (i.e. more than two). I know
there's a string I could use as an argument for ToString
() that will only display the first two deigits after the
decimal point, does anyone know what that is? Thanks for
your help!

decimal AccountTotal = 4.7300;

string myString = AccountTotal.ToString("????");


Nov 18 '05 #2
Gregory,

Thank you so much for the great explanation! It is
extremely helpful. I am using early binding, so the
DataBinder won't work, but how would I get it to display
the amount to two decimal places without the "$"? Thanks
so much for your help!
-----Original Message-----
The .#### is normal for money types in SQL Server. For output in an ASP.NETapp, you can use DataBinder.Eval to output. The number will still have 4,but only show 2.

<%# DataBinder.Eval (Container.DataItem, "Price", "{0:c}") %>
This involves late binding, and may not work for all situations. Now, if youwish to include the $ or other country formatting, you can do this trick:
CurrencyLabel.Text = doubleVal.ToString("C",
Thread.CurrentThread.CurrentCulture);

If it is always dollars, you can use:

CurrencyLabel.Text = doubleVal.ToString("C", new CultureInfo("en-us"));
NOTE that I am using a double here. If you use
System.Data.SqlTypes.SqlMoney, you will ALWAYS have a precision of 4.
--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************* ******** *************Think Outside the Box!
************************************************* ******** *************"Mark Fox" <in**@solelsoftware.com> wrote in message
news:10****************************@phx.gbl...
Hello,

I have a "smallmoney" field in SQL Server that I am attempting to display, but when I use the .ToString()
method on a decimal variable it displays too many places after the decimal point (i.e. more than two). I know
there's a string I could use as an argument for ToString () that will only display the first two deigits after the decimal point, does anyone know what that is? Thanks for your help!

decimal AccountTotal = 4.7300;

string myString = AccountTotal.ToString("????");


.

Nov 18 '05 #3
Hi Mark,

You can consider of the String.Format method. For example,

String.Format(AccountTotal.ToString(), "{0:c}");

If I have misunderstood your concern, please feel free to let me know.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Ken | last post by:
Hi. Can anyone refer me to any articles about the compatibility between c++ polymorphism and real-time programming? I'm currently on a real-time c++ project, and we're having a discussion...
6
by: Paul | last post by:
In real life situation, do we ever come across a situation where we would need two base objects in an object. A snippet is worth 1000 words (: so... class Base { }; class Derived1:public Base...
3
by: Gérard Talbot | last post by:
Hello all, When webfonts are used for purely cosmetic/ornemental reasons and on a large scale, I don't agree. When webfonts are used because Unicode support among browsers for a particular...
3
by: Mike | last post by:
Hello In the following code,why are there 2 different Class1's.Is one a class definition and the other a function? namespace Music { public class Class1 { public Class1() {
30
by: Raymond Hettinger | last post by:
Proposal -------- I am gathering data to evaluate a request for an alternate version of itertools.izip() with a None fill-in feature like that for the built-in map() function: >>> map(None,...
12
by: Raymond Hettinger | last post by:
I am evaluating a request for an alternate version of itertools.izip() that has a None fill-in feature like the built-in map function: >>> map(None, 'abc', '12345') # demonstrate map's None...
34
by: Guch Wu | last post by:
Boost has many terrific libraries. But I want to know whether they are ready for using in real projects. Which of them are mature enough, or just only in progress?
9
by: d.adamkiewicz | last post by:
Hello Folks Anybody can show me real world singleton class example? Something that works (is implemented) as part of working solution. Does it make sense to create database handler class that...
14
by: dE|_ | last post by:
For months I have been happily using a downloaded JS file and inline script to embed and control the parameters of SWF files which I use as mp3 players with actionscript. The last so.addParam...
22
by: Bill Reid | last post by:
I just noticed that my "improved" version of sscanf() doesn't assign floating point numbers properly if the variable assigned to is declared as a "float" rather than a "double". (This never...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.