473,396 Members | 1,938 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,396 software developers and data experts.

float to Money cast

Hi,
I have the following:
float.Parse(myproduct.Price.Value.ToString());

myproduct.Price.Value.ToString() returns $24.00 (with the $)

Is there anyway to do the above cast, I know the float.Parse(...)
doesn't like the $, as i keep getting an invalid cast exception, is
there anyway to get this to work? Do i just need to strip out the $
with substring, or is there a better way to cast it?

Thanks.

Apr 9 '07 #1
15 4022
First off - I'd be using decimal (never float) to store money...
Second... one of the overloads to decimal.Parse accepts a number-style
param, one of which is currency, e.g.
>?decimal.Parse("£21.12", System.Globalization.NumberStyles.Currency);
21.12
Marc

Apr 9 '07 #2
Completely out of curiosity... what is the .Price.Value typed as? Just
wondering if the ToString() is making your life harder without good
reason...

Marc

Apr 9 '07 #3
When you call the Parse method, pass the Currency value from the
NumberStyles enumeration as the second parameter. It should perform the
parse operation correctly then.

Also, technically you are not performing a cast, but you are performing
a conversion. There is a difference.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<so******@yahoo.comwrote in message
news:11**********************@e65g2000hsc.googlegr oups.com...
Hi,
I have the following:
float.Parse(myproduct.Price.Value.ToString());

myproduct.Price.Value.ToString() returns $24.00 (with the $)

Is there anyway to do the above cast, I know the float.Parse(...)
doesn't like the $, as i keep getting an invalid cast exception, is
there anyway to get this to work? Do i just need to strip out the $
with substring, or is there a better way to cast it?

Thanks.

Apr 9 '07 #4
so******@yahoo.com wrote:
I have the following:
float.Parse(myproduct.Price.Value.ToString());

myproduct.Price.Value.ToString() returns $24.00 (with the $)

Is there anyway to do the above cast, I know the float.Parse(...)
doesn't like the $, as i keep getting an invalid cast exception, is
there anyway to get this to work? Do i just need to strip out the $
with substring, or is there a better way to cast it?
I would think that:

Convert.ToSingle(myproduct.Price.Value)

would be a better approach.

But I warn you - a future version of .NET will contain a feature
where if a programmer ever tries to use float data type for money,
then a baseball bat will jump out of the monitor and beat you up ...

:-)

Floating point (float and double) is a no no for amounts.

Arne

Apr 9 '07 #5
Arne Vajhøj <ar**@vajhoej.dkwrote:

<snip>
Floating point (float and double) is a no no for amounts.
Small piece of pedantry - decimal is also a floating point type. It's a
floating *decimal* point type, however, rather than a floating *binary*
point type.

I completely agree that using float or double is wrong for money,
however :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 10 '07 #6
Jon Skeet [C# MVP] wrote:
Arne Vajhøj <ar**@vajhoej.dkwrote:
>Floating point (float and double) is a no no for amounts.

Small piece of pedantry - decimal is also a floating point type. It's a
floating *decimal* point type, however, rather than a floating *binary*
point type.

I completely agree that using float or double is wrong for money,
however :)
I thought decimal was fixed point.

But you are right. It is floating point. Yuck.

Arne

Apr 10 '07 #7
Arne Vajhøj <ar**@vajhoej.dkwrote:
Jon Skeet [C# MVP] wrote:
Arne Vajhøj <ar**@vajhoej.dkwrote:
Floating point (float and double) is a no no for amounts.
Small piece of pedantry - decimal is also a floating point type. It's a
floating *decimal* point type, however, rather than a floating *binary*
point type.

I completely agree that using float or double is wrong for money,
however :)
I thought decimal was fixed point.

But you are right. It is floating point. Yuck.
I don't see why that's particularly "yucky". If it were going to be
fixed point, where would you fix the point? It's okay to have it fixed
point in SQL, because every field can fix it in a different place
(well, size/precision).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 10 '07 #8
Jon Skeet [C# MVP] wrote:
Arne Vajhøj <ar**@vajhoej.dkwrote:
>Jon Skeet [C# MVP] wrote:
>>Arne Vajhøj <ar**@vajhoej.dkwrote:
Floating point (float and double) is a no no for amounts.
Small piece of pedantry - decimal is also a floating point type. It's a
floating *decimal* point type, however, rather than a floating *binary*
point type.

I completely agree that using float or double is wrong for money,
however :)
I thought decimal was fixed point.

But you are right. It is floating point. Yuck.

I don't see why that's particularly "yucky". If it were going to be
fixed point, where would you fix the point? It's okay to have it fixed
point in SQL, because every field can fix it in a different place
(well, size/precision).
With money I would prefer it to keep decimals.

1.01m / 10 to give 0.10m and not 0.101m !

Arne
Apr 10 '07 #9
On Mon, 09 Apr 2007 18:32:39 -0700, Arne Vajhøj <ar**@vajhoej.dkwrote:
With money I would prefer it to keep decimals.

1.01m / 10 to give 0.10m and not 0.101m !
It is trivial to accomplish that when desired, but if you have a need to
maintain fractional units of currency beyond two decimal places and the
data type doesn't offer that, it gets a lot more complicated. Better that
the language offer more functionality than you need than to unnecessarily
restrict you.

I think it makes perfect sense for decimal to be floating point. Lots of
financial applications are not naturally limited to two decimal places,
whether simply because of the nature of the transaction, or because there
is some currency exchange or conversion going on.

Pete
Apr 10 '07 #10
Arne,

I think that you are confusing what Jon is trying to say somewhat.
There is nothing wrong with a floating point type. The issue with using a
binary representation of a floating point type in computers is the
inaccuracy that results from using them.

However, the decimal type doesn't suffer from this inaccuracy. Because
of this, it shouldn't be an issue whether or not it is a floating point
type. The decimal is precise, and that is what matters in this situation
(everyone is precise with their money, no?).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Arne Vajhøj" <ar**@vajhoej.dkwrote in message
news:46***********************@news.sunsite.dk...
Jon Skeet [C# MVP] wrote:
>Arne Vajhøj <ar**@vajhoej.dkwrote:
>>Jon Skeet [C# MVP] wrote:
Arne Vajhøj <ar**@vajhoej.dkwrote:
Floating point (float and double) is a no no for amounts.
Small piece of pedantry - decimal is also a floating point type. It's a
floating *decimal* point type, however, rather than a floating *binary*
point type.

I completely agree that using float or double is wrong for money,
however :)
I thought decimal was fixed point.

But you are right. It is floating point. Yuck.

I don't see why that's particularly "yucky". If it were going to be fixed
point, where would you fix the point? It's okay to have it fixed point in
SQL, because every field can fix it in a different place (well,
size/precision).

With money I would prefer it to keep decimals.

1.01m / 10 to give 0.10m and not 0.101m !

Arne

Apr 10 '07 #11
Peter Duniho wrote:
On Mon, 09 Apr 2007 18:32:39 -0700, Arne Vajhøj <ar**@vajhoej.dkwrote:
>With money I would prefer it to keep decimals.

1.01m / 10 to give 0.10m and not 0.101m !

It is trivial to accomplish that when desired, but if you have a need to
maintain fractional units of currency beyond two decimal places and the
data type doesn't offer that, it gets a lot more complicated. Better
that the language offer more functionality than you need than to
unnecessarily restrict you.
You think specifying length and index in the declarations is more
complicated than calling Math.Round on the expressions. I don't.
I think it makes perfect sense for decimal to be floating point. Lots
of financial applications are not naturally limited to two decimal
places, whether simply because of the nature of the transaction, or
because there is some currency exchange or conversion going on.
It does not need to be fixed at two decimals. I am not aware
of any fixed point that are so.

Arne
Apr 12 '07 #12
Nicholas Paldino [.NET/C# MVP] wrote:
Arne,

I think that you are confusing what Jon is trying to say somewhat.
There is nothing wrong with a floating point type. The issue with using a
binary representation of a floating point type in computers is the
inaccuracy that results from using them.

However, the decimal type doesn't suffer from this inaccuracy. Because
of this, it shouldn't be an issue whether or not it is a floating point
type. The decimal is precise, and that is what matters in this situation
(everyone is precise with their money, no?).
No.

There are more than just that problem with floating point.

Don't show this program to your accountant:

using System;

namespace E
{
public class MainClass
{
public static void Main(string[] args)
{
Console.WriteLine(decimal.MaxValue - (decimal.MaxValue - 0.10m));
Console.ReadLine();
}
}
}

The practical problem is not so big because decimal is so huge as it is,
but it has some of the classic floating point characteristics.

Arne
Apr 12 '07 #13
Arne Vajhøj wrote:
The practical problem is not so big because decimal is so huge as it is,
but it has some of the classic floating point characteristics.
And don't get me wrong.

I am really not saying that is a bad design.

I am just very surprised.

I always thought decimal was similar to SQL DECIMAL
and COBOL COMP-3.

Arne
Apr 12 '07 #14
On Wed, 11 Apr 2007 18:04:30 -0700, Arne Vajhøj <ar**@vajhoej.dkwrote:
It does not need to be fixed at two decimals. I am not aware
of any fixed point that are so.
I'm aware of plenty of fixed point types where you don't get to pick the
precision of the type.

That said, whatever. I find the .NET decimal type just fine. YMMV.
Apr 12 '07 #15
Arne Vajh?j <ar**@vajhoej.dkwrote:
It is trivial to accomplish that when desired, but if you have a need to
maintain fractional units of currency beyond two decimal places and the
data type doesn't offer that, it gets a lot more complicated. Better
that the language offer more functionality than you need than to
unnecessarily restrict you.

You think specifying length and index in the declarations is more
complicated than calling Math.Round on the expressions. I don't.
It's a complete change to the type system to be able to specify
precision/scale as part of a type declaration. That sort of type
declaration is already part of SQL, but it's nowhere to be found in C#
or .NET itself. That's a huge disruption. Calling Math.Round certainly
isn't.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 12 '07 #16

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

Similar topics

3
by: David Sharp | last post by:
I'm trying to find a way to format a FLOAT variable into a varchar in SQL Server 2000 but using CAST/CONVERT I can only get scientific notation i.e. 1e+006 instead of 1000000 which isn't really...
5
by: Peter Scheurer | last post by:
Hi, we found some strange behavior when operating with floats and round(). The following simplified statement reproduces the problem. select 6.56 - round(convert(float, 6.56), 2) from...
4
by: jaijai_kumar | last post by:
Select Cast('100.1234' as float) give me the result 100.1234 Now when I convert it back to char I want exactly 100.1234 Select Convert(char(100),Cast('100.1234' as float)) Gives me 100.123 (Here...
6
by: Dave win | last post by:
Hi all: I'm confused with the expression "(float *())". Book says that this is a cast. But, I have no idea of this expr. why could this expr ignore the variable??? Thanx!!!
19
by: Jon Shemitz | last post by:
Is there a difference between a constant like "12.34f" and "(float) 12.34"? In principle, at least, the latter is a double constant being cast to a float; while the two both generate actual...
4
by: Jerry | last post by:
Is it possible to turn off the "$" and "," that appear in "money" formatted columns so I can dump the table in a numeric format? The man page hints that lc_monetary controls the formatting but I...
9
by: Nicolas Blais | last post by:
Hi, I have this following class which I use as a timer: #include <sys/time.h> using namespace std; class chrono { public: chrono() {};
15
by: k3n3dy | last post by:
Hello everybody, I would like to ask anybody to help me understand what should be done exactly with this Problem. I have provided what I have up to now, although it is not quite accurate. Create...
3
by: Arnie | last post by:
Folks, We ran into a pretty significant performance penalty when casting floats. We've identified a code workaround that we wanted to pass along but also was wondering if others had experience...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.