Deepa Hepzibah
Deepa Hepzibah

Reputation: 55

Convert String to System.Decimal in java

I have a need of inserting into variable which is of type System.Decimal through java code. I tried using float and double in java. but seems like it is not accepting.

Any idea which is the equivalent datatype in java to datatype of System.Decimal in C#.

Can anyone help me out?

Upvotes: 0

Views: 433

Answers (3)

GerritCap
GerritCap

Reputation: 1616

Why do you need a C# System.Decimal in a Java application ????

Do you want an object instead of a primitive type float or double, then you have the class java.lang.Double. Using autoboxing of Java you can write code like

Double d = 0.3;

or not using autoboxing you can write

Double d = new Double(0.3);

if you have a String then you can use

Double d = Double.valueOf("0.3");

The class Double as an object class or the type double as primitive have the same properties regarding precision etc... if you want higher precision you can use the BigDecimal class.

BigDecimal bd = new BigDecimal("0.3");

Upvotes: 1

Eric
Eric

Reputation: 1297

I'm assuming you have a String and want to convert it into a format that supports decimal values...

String value = "12.34";
Float f = Float.valueOf(value);

Upvotes: 0

Joey
Joey

Reputation: 354416

The best equivalent would probably be BigDecimal. It't arbitrary-precision instead of the fixed precision of System.Decimal but it's exact decimal arithmetic.

Upvotes: 3

Related Questions