zey
zey

Reputation: 6103

float to string with no decimal value in C#

This is how I get previous greatest ID from my datasource and plus one to this value .

   string _fID = (float.Parse("." + DBContext.Employees.OrderByDescending(x =>   
    x.EmpID).FirstOrDefault().EmpID.ToString()) + .00001f).ToString("F5");

And I get _fID = 0.00002 .

But what I want is the string with no decimal value .
Eg. _fID = 00002 .

What I've done is replacing like _fID = _fID.Replace("0.",""); .
Is there any short way to make this stuff ? Thanks you all :)

PS

Data Type of EmpID is nvarchar(5) .

Upvotes: 0

Views: 2557

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1503629

I would suggest you stop using floating point types at all. Just use integers, as that's basically what you've got. (It's a shame that your data type is textual when you're logically just using integers.)

string currentHighest = DBContext.Employees
                                 .Select(x => x.EmpID)
                                 .OrderByDescending(x => x)
                                 .FirstOrDefault();
// TODO: Handle currentHighest being null (empty table)
int nextId = (int.Parse(currentHighest) + 1).ToString("00000");

Upvotes: 7

Related Questions