Developer forum

Forum » CMS - Standard features » Format - what am I missing?

Format - what am I missing?

Hans Ravnsfjall
Hans Ravnsfjall
Reply

If i do this in HTML, it works fine

<!--@Ecom:Product:Field.AskingPrice.Value.FormatDouble({0:N})-->

But if i do this in Razor, I get no result.

@GetValue("Ecom:Product:Field.AskingPrice.Value.FormatDouble({0:N})")

What am I doing wrong, or Am I missing something?


Replies

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

FormatDouble is a tag extension, not a real tag and as such won't work in Razor as-is. You don't need it though as you can use native .NET functionality. If you replace @GetValue with @GetDouble (or @Decimal, I forgot) you get back a real .NET numeric type which you can then call ToString on to format the number any way you want.

Cheers,

Imar

 
Nicolai Pedersen
Reply

So that would be @GetDouble("Ecom:Product:Field.AskingPrice.Value").ToString("{0:N}")

 
Hans Ravnsfjall
Hans Ravnsfjall
Reply

Thank you very Mutch. This doesn´t quite work tough Nicolai. The output i get from this is {1500000:N}

It does not seem to format correctly.

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Since you're calling it on the double itself, you don't need the curly braces (which are normally used in string.Format). This should work:

@GetDouble("Ecom:Product:Field.AskingPrice.Value").ToString("N")

 
Hans Ravnsfjall
Hans Ravnsfjall
Reply

Thanks for the reply Imar

That only outputs  0,00

So now the format is ok, but the value is gone.

 

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Depending on what AskingPrice is, you may need to use AskingPrice.Price instead of .Value. Here's what I have in my template:

@product.GetDouble("Ecom:Product.Price.Price") // Outputs 117.93
@product.GetDouble("Ecom:Product.Price.Value") // Outputs 0
@product.GetDouble("Ecom:Product.Price") // Outputs 0
@product.GetString("Ecom:Product.Price") // Outputs $117.93

 
Hans Ravnsfjall
Hans Ravnsfjall
Reply

Ok

I get the value from a Custom Field in eCom. Field type is Integer (think it is int32).

I tried your examples Imar, but I still only get 0,00

Could it be something to do with that I am not using the Dynamciweb default price field? Unfortunately I can not use the Default price field only, because I have to show many different prices (Aksing Price, Latest Bid, etc.)

 

 

 
Hans Ravnsfjall
Hans Ravnsfjall
Reply

If I do this @GetInteger("Ecom:Product:Field.AskingPrice.Value") - i get 0. Isn´t that a bit strange, since the value I am parsing is an integer - and the value is not 0 but eg. 1500000?

 

@GetDouble("Ecom:Product:Field.AskingPrice.Value") also outputs 0

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Interesting, I see different behavior. I created two new product fields (not category fields), one an Integer, the other a double and assigned them 19 and 19.53 respectively. I then get the following outout in my template:

@product.GetInteger("Ecom:Product:Field.TestInt").ToString("N") // 19.00
@product.GetDouble("Ecom:Product:Field.TestInt").ToString("N")  // 19.00
@product.GetString("Ecom:Product:Field.TestInt") //19 

@product.GetInteger("Ecom:Product:Field.TestDouble").ToString("N") // 0.00
@product.GetDouble("Ecom:Product:Field.TestDouble").ToString("N") // 19.53
@product.GetString("Ecom:Product:Field.TestDouble") //19.53


The double to integer conversion resulting in zero is a little odd but other than that I think this looks like what you'd expect.

Is this what you have as well? If not, can you describe your fields, and template a bit more?

 

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply
This post has been marked as an answer

BTW: where are you calling this? In a details page or a list? If in a list, wouldn't you need to call GetDouble on the item you're foreach'ing over?

Votes for this answer: 1
 
Hans Ravnsfjall
Hans Ravnsfjall
Reply

Spot on Imar. Problem was I was in a loop, and didn´t call the value in the loop context.

 

Thank you very mutch for the help, and sorry for the inconvenience.

 

One final question. If i don´t want to show the comma, and the 2 decimals (,00). How do I do that?

 

br. Hans

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Then don't call ToString and just output the value of GetInteger directly:

@product.GetInteger("Ecom:Product:Field.TestInt") // 19

 
Hans Ravnsfjall
Hans Ravnsfjall
Reply

Thank you Imar, but what will just ouptul 1500000

I still want the format 1.500.000

I just don´t want the last ,00

 

Would I have to add something like .Substring(0, input.LastIndexOf(","));

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Does this work for you?

@product.GetInteger("Ecom:Product:Field.TestInt").ToString("N0")

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

BTW: for more info on this: https://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx

 
Hans Ravnsfjall
Hans Ravnsfjall
Reply

Thank you very mutch for your help Imar. Have a very good day, and a very merru christmas festives :)

 

You must be logged in to post in the forum