Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

decimal
c#
method
tostring
communitycreator

What is the Decimal.ToSingle() method in C#?

Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The Decimal.ToSingle() method converts a specified decimal value to its equivalent single-precision, floating-point number.

Syntax

public static float ToSingle (decimal d);

Parameters

d: This is the decimal number that we want to convert to a single-precision, floating-point number.

Return value

The value returned is a single-precision, floating-point number that is equivalent to the value of d given in the syntax above.

Code example

// use System
using System;
// create class
class DecimalToSingle
{
// main method
static void Main()
{
// create decimal
decimal d1 = 34.222m;
decimal d2 = 8.9999m;
decimal d3 = -5.8m;
// convert to Single
// and print value
Console.WriteLine("Value : {0}, type: {1}",
Decimal.ToSingle(d1),
Decimal.ToSingle(d1).GetType());
Console.WriteLine("Value : {0}, type: {1}",
Decimal.ToSingle(d2),
Decimal.ToSingle(d2).GetType());
Console.WriteLine("Value : {0}, type: {1}",
Decimal.ToSingle(d3),
Decimal.ToSingle(d2).GetType());
}
}
Convert decimal values to single-precision floating point numbers

Code explanation

  • Lines 10–12: We create some decimal variables, d1, d2, and d3, and initialize them with decimal values.
  • Lines 16–24: We print the results of converting d1, d2, and d3 and the value that is returned. We also print the instance type by using the GetType() method.

RELATED TAGS

decimal
c#
method
tostring
communitycreator

CONTRIBUTOR

Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring